home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / xlibpas2.zip / XLIB2.ASM < prev    next >
Assembly Source File  |  1994-06-12  |  75KB  |  4,749 lines

  1. ; ***************************************************************************
  2. ; **                             XLibPas v2.0                              **
  3. ; **                        for Borland Pascal 7.0                         **
  4. ; **                                  by                                   **
  5. ; **                            Tristan Tarrant                            **
  6. ; ***************************************************************************
  7. ; ** Credits :                                                             **
  8. ; **   Michael Abrash    - Concepts, Algorithms                            **
  9. ; **   Themie Gouthas    - Main code and implementation                    **
  10. ; **   Matthew MacKenzie - Compiled Bitmaps, Circles, etc.                 **
  11. ; **   Tore Bastiansen   - Virtual VSync Handler                           **
  12. ; ***************************************************************************
  13.  
  14.         .MODEL TPASCAL
  15.         .386
  16.         LOCALS
  17.  
  18. include xlib2.inc
  19.         .DATA
  20.  
  21.     OldHandlerSeg      dw  ?
  22.     OldHandlerOffs     dw  ?
  23.     OldHandlerMask     dw  ?
  24.     OldX               dw  ?
  25.     OldY               dw  ?
  26.     OldScrnOffs        dw  ?
  27.  
  28.     level              dw  ?
  29.     gencount           dw  ?
  30.     AdjustedOffs       dw  ?
  31.     p1xt               dw  ?
  32.     p1yt               dw  ?
  33.     p2xt               dw  ?
  34.     p2yt               dw  ?
  35.     p4xt               dw  ?
  36.     p4yt               dw  ?
  37.     p5xt               dw  ?
  38.     p5yt               dw  ?
  39.     c1xt               dw  ?
  40.     c1yt               dw  ?
  41.     c2xt               dw  ?
  42.     c2yt               dw  ?
  43.     xdiff              dw  ?
  44.     ydiff              dw  ?
  45.     moveline           dw  ?
  46.     stepper            dw  ?
  47.     bptemp             dw  ?
  48.     Colort             dw  ?
  49.  
  50.     TicksPerSecond         dw ?
  51.     VsyncIntTicks          label dword
  52.     VsyncIntTicksLo        dw ?
  53.     VsyncIntTicksHi        dw ?
  54.     VsyncPeriod            dw ?       ;Time (in clicks) between each vsync
  55.                                                                         ;1 click = 1.193 microseconds
  56.  
  57.     ClockRate              dw ?             ;Clock rate (in clicks) for timer 0
  58.     ClockCounter           dw ?             ;Counts total clicks modulo 65536
  59.     UserVsyncHandler       label dword      ;Pointer to user routine called
  60.     UserVsyncOffs          dw ?             ;called once each vsync period.
  61.     UserVsyncSeg           dw ?
  62.     InUserHandler          dw ?
  63.                 db 100h dup(?)
  64.     LocalStack             label byte       ;Local stack for user handler
  65.     StackSeg               dw ?
  66.     StackPtr               dw ?
  67.  
  68.  
  69.     ElapsedVrts            dw ?
  70.     VrtsToSkip             dw ?
  71.     MouseVSyncHandler      dd ?
  72.  
  73.  
  74.         .CODE
  75.  
  76. XSetMode PROC FAR Mode : Word, WidthInPixels : Word
  77.  
  78.     mov InGraphics,0
  79.     mov ErrorValue,0
  80.     mov DoubleScanFlag,0
  81.     mov CurrXMode,0
  82.     mov ScrnPhysicalByteWidth,0
  83.     mov ScrnPhysicalPixelWidth,0
  84.     mov ScrnPhysicalHeight,0
  85.     mov SplitScrnOffs,0
  86.     mov SplitScrnScanLine,0
  87.     mov SplitScrnVisibleHeight,0
  88.     mov SplitScrnActive,0
  89.     mov Page0Offs,0
  90.     mov Page1Offs,0
  91.     mov Page2Offs,0
  92.     mov ScrnLogicalByteWidth,0
  93.     mov ScrnLogicalPixelWidth,0
  94.     mov ScrnLogicalHeight,0
  95.     mov MaxScrollX,0
  96.     mov MaxScrollY,0
  97.     mov DoubleBufferActive,0
  98.     mov VisiblePageIdx,0
  99.     mov HiddenPageOffs,0
  100.     mov VisiblePageOffs,0
  101.     mov NonVisualOffs,0
  102.     mov TopClip,0
  103.     mov BottomClip,0
  104.     mov LeftClip,0
  105.     mov RightClip,0
  106.     mov PhysicalStartPixelX,0
  107.     mov PhysicalStartByteX,0
  108.     mov PhysicalStartY,0
  109. IFDEF _DPMI_
  110.     mov    ax,__AHIncr
  111.     mov SelectorInc,ax
  112.     mov ax, __0000H
  113.     mov Seg0000,ax
  114.     mov ax, __A000H
  115. ELSE
  116.     mov ax, 0A000h
  117. ENDIF
  118.     mov ScreenSeg,ax
  119.     cld
  120.     mov   ax,ds
  121.     mov   es,ax
  122.     mov   cx,mode
  123.     cmp   cx,LastXMode
  124.     jle   @@ValidMode
  125.     mov   InGraphics,0
  126.     mov   ax,-1
  127.     jmp @@Done
  128.  
  129. @@ValidMode:
  130.     mov   CurrXMode,cx
  131.     mov   InGraphics,1
  132.     xor   al,al
  133.     cmp   cx,3
  134.     jg    @@SetDoubleScanFlag
  135.     mov   al,1
  136.  
  137. @@SetDoubleScanFlag:
  138.     mov   DoubleScanFlag,al
  139.     push  cx
  140.     mov   ax,13h
  141.     int   10h
  142.     pop   cx
  143.     mov   dx,ScIndex
  144.     mov   ax,0604h
  145.     out   dx,ax
  146.     mov   ax,0100h
  147.     out   dx,ax
  148.     mov   bx,offset ModeTable
  149.     shl   cx,1
  150.     add   bx,cx
  151.     mov   si, word ptr [bx]
  152.     lodsb
  153.     or    al,al
  154.     jz    @@DontSetDot
  155.     mov   dx,MISCOUTPUT
  156.     out   dx,al
  157.  
  158. @@DontSetDot:
  159.     mov   dx,ScIndex
  160.     mov   ax,0300h
  161.     out   dx,ax
  162.     mov   dx,CrtcIndex
  163.     mov   al,11h
  164.     out   dx,al
  165.     inc   dx
  166.     in    al,dx
  167.     and   al,07fh
  168.     out   dx,al
  169.     dec   dx
  170.     cld
  171.     xor   cx,cx
  172.     lodsb
  173.     mov   cl,al
  174.  
  175. @@SetCRTParmsLoop:
  176.     lodsw
  177.  
  178.     out   dx,ax
  179.     loop  @@SetCRTParmsLoop
  180.     mov   dx,ScIndex
  181.     mov   ax,0f02h
  182.     out   dx,ax
  183.     mov   ax,ScreenSeg
  184.     mov   es,ax
  185.     sub   di,di
  186.     sub   ax,ax
  187.     mov   cx,8000h
  188.     rep   stosw
  189.     lodsw
  190.     mov   ScrnPhysicalPixelWidth,ax
  191.     mov   SplitScrnScanLine,ax
  192.     mov   bx,ax
  193.     shr   ax,2
  194.     mov   ScrnPhysicalByteWidth,ax
  195.     lodsw
  196.     mov   ScrnPhysicalHeight,ax
  197.     mov   cx,widthinpixels
  198.     mov   dx,CrtcIndex
  199.     mov   al,CRTCOFFSET
  200.     out   dx,al
  201.     inc   dx
  202.     mov   ax,cx
  203.     cmp   ax,ScrnPhysicalPixelWidth
  204.     jge   @@ValidLogicalWidth
  205.     mov   ax,bx
  206.  
  207. @@ValidLogicalWidth:
  208.     shr   ax,3
  209.     out   dx,al
  210.     shl   ax,1
  211.     mov   bx,ax
  212.     mov   ScrnLogicalByteWidth,ax
  213.     mov   RightClip,ax
  214.     sub   ax,ScrnPhysicalByteWidth
  215.     shl   ax,2
  216.     mov   MaxScrollX,ax
  217.     mov   ax,bx
  218.     shl   ax,2
  219.     mov   ScrnLogicalPixelWidth,ax
  220.     mov   cx,ax
  221.     mov   ax,0ffffh
  222.     sub   dx,dx
  223.     div   bx
  224.     mov   ScrnLogicalHeight,ax
  225.     mov   BottomClip,ax
  226.     sub   ax,ScrnPhysicalHeight
  227.     mov   MaxScrollY,ax
  228.     mov   ax,cx
  229.     mov   ax,ScrnLogicalByteWidth
  230.     mul   ScrnPhysicalHeight
  231.     mov   NonVisualOffs,ax
  232.  
  233. @@Done:
  234.     mov   ax, 0CB00h
  235.     int   10h
  236.     ret
  237.  
  238. XSetMode ENDP
  239.  
  240. XSelectDefaultPlane PROC FAR Plane : Byte
  241.  
  242.     mov  cl,Plane
  243.     and  cl,011b
  244.     mov  ax,0100h + MapMask
  245.     shl  ah,cl
  246.     mov  dx,ScIndex
  247.     out  dx,ax
  248.     mov  ah,cl
  249.     mov  al,ReadMap
  250.     mov  dx,GcIndex
  251.     out  dx,ax
  252.     ret
  253.  
  254. XSelectDefaultPlane ENDP
  255.  
  256. XSetSplitscreen PROC FAR Line : Word
  257.  
  258.     xor  si,si
  259.     cmp  DoubleBufferActive,0
  260.     jne  @@error
  261.     cmp  SplitScrnActive,0
  262.     je   @@NotPreviouslyCalled
  263.  
  264. @@error:
  265.     mov  ErrorValue,ERROR
  266.     ret
  267.  
  268. @@NotPreviouslyCalled:
  269.     mov  dx,InputStatus0
  270.     in   al,dx
  271.     mov  al,ACMODECONTROL+20h
  272.     mov  dx,ACINDEX
  273.     out  dx,al
  274.     inc  dx
  275.     in   al,dx
  276.     or   al,20h
  277.     dec  dx
  278.     out  dx,al
  279.     mov  PhysicalStartByteX,ax
  280.     mov  PhysicalStartPixelX,ax
  281.     mov  PhysicalStartY,ax
  282.     mov  SplitScrnActive,1
  283.     mov  ax,Line
  284.     jns  @@NotNeg
  285.     mov  ax,0
  286.  
  287. @@NotNeg:
  288.     mov  SplitScrnScanLine,ax
  289.     or   DoubleScanFlag,0
  290.     jz   @@NotDoubleScanned
  291.     shl  ax,1
  292.     dec  ax
  293.  
  294. @@NotDoubleScanned:
  295.     mov  bx,ax
  296.     mov  dx,InputStatus0
  297.  
  298. @@WaitNotVsync:
  299.     in   al,dx
  300.     test al,08h
  301.     jnz  @@WaitNotVsync
  302.  
  303. @@WaitVsync:
  304.     in   al,dx
  305.     test al,08h
  306.     jz   @@WaitVsync
  307.     cli
  308.     mov  dx,CrtcIndex
  309.     mov  ah,bl
  310.     mov  al,LINECOMPARE
  311.     out  dx,ax
  312.     mov  ah,bh
  313.     and  ah,1
  314.     shl  ah,4
  315.     mov  al,Overflow
  316.     out  dx,al
  317.     inc  dx
  318.     in   al,dx
  319.     and  al, not 10h
  320.     or   al,ah
  321.     out  dx,al
  322.     dec  dx
  323.     mov  ah,bh
  324.     and  ah,2
  325.     ror  ah,3
  326.     mov  al,MAXSCANLINE
  327.     out  dx,al
  328.     inc  dx
  329.     in   al,dx
  330.     and  al, not 40h
  331.     or   al,ah
  332.     out  dx,al
  333.     sti
  334.     mov  ax,ScrnPhysicalHeight
  335.     sub  ax,SplitScrnScanLine
  336.     mov  SplitScrnVisibleHeight,ax
  337.     mov  bx,ScrnLogicalByteWidth
  338.     mul  bx
  339.     mov  Page0Offs,ax
  340.     mov  Page1Offs,ax
  341.     mov  Page2Offs,ax
  342.     mov  cx,0ffffh
  343.     sub  cx,ax
  344.     xchg cx,ax
  345.     sub  dx,dx
  346.     div  bx
  347.     mov  ScrnLogicalHeight,ax
  348.     cmp  ax,BottomClip
  349.     jle  @@BottomClipOK
  350.     mov  BottomClip,ax
  351.  
  352. @@BottomClipOK:
  353.     sub  ax,SplitScrnScanLine
  354.     mov  MaxScrollY,ax
  355.     xchg cx,ax
  356.     mov  bh,al
  357.     mov  ch,ah
  358.     mov  bl,ADDRLOW
  359.     mov  cl,ADDRHIGH
  360.     and  si,0003h
  361.     mov  ah,byte ptr PelPanMask[si]
  362.     mov  al,PELPANNING+20h
  363.     mov  si,ax
  364.  
  365. ; VSync Handler stuff
  366.  
  367.     cmp  VSyncHandlerActive, TRUE
  368.     jne   @@NoVsyncHandler
  369.  
  370. @@WaitLast:
  371.     cmp   StartAddressFlag,0
  372.     jne   @@WaitLast
  373.     cli
  374.     mov  WaitingStartLow,bx
  375.     mov  WaitingStartHigh,cx
  376.     mov  WaitingPelPan,si
  377.     mov  StartAddressFlag,1
  378.     sti
  379.     jmp  short @@Return
  380.  
  381. ; End VSync Handler stuff
  382.  
  383. @@NoVsyncHandler:
  384.     mov  dx,InputStatus0
  385.  
  386. @@WaitDE:
  387.     in   al,dx
  388.     test al,01h
  389.     jnz  @@WaitDE
  390.     mov  dx,CrtcIndex
  391.     mov  ax,bx
  392.     cli
  393.     out  dx,ax
  394.     mov  ax,cx
  395.     out  dx,ax
  396.     sti
  397.     mov  dx,InputStatus0
  398.  
  399. @@WaitVS:
  400.     in   al,dx
  401.     test al,08h
  402.     jz @@WaitVS
  403.  
  404.     mov  dx,ACINDEX
  405.     mov  ax,si
  406.     cli
  407.     out  dx,al
  408.     mov  al,ah
  409.     out  dx,al
  410.     sti
  411.  
  412. @@Return:
  413.     mov  ErrorValue,OK
  414.     ret
  415.  
  416. XSetSplitScreen ENDP
  417.  
  418. XSetStartAddr PROC FAR X : Word, Y : Word
  419.  
  420.     mov  si,x
  421.     mov  ax,ScrnLogicalByteWidth
  422.     mov  cx,y
  423.     mul  cx
  424.     cmp  DoubleBufferActive,1
  425.     je   @@PageResolution
  426.     cmp  TripleBufferActive,1
  427.     je   @@PageResolution
  428.  
  429. @@PageFlipEntry1:
  430.     add  ax,Page0Offs
  431.     jmp  @@AddColumn
  432.  
  433. @@PageFlipEntry2:
  434.     mov  PhysicalStartPixelX,si
  435.     mov  PhysicalStartY,cx
  436.  
  437. @@PageResolution:
  438.     add  ax,VisiblePageOffs
  439.  
  440. @@AddColumn:
  441.     mov  cx,si
  442.     shr  cx,2
  443.     mov  PhysicalStartByteX,cx
  444.     add  ax,cx
  445.     mov  bh,al
  446.     mov  ch,ah
  447.  
  448. @@StartAddrEntry:
  449.     mov  bl,ADDRLOW
  450.     mov  cl,ADDRHIGH
  451.     and  si,0003h
  452.     mov  ah,byte ptr PelPanMask[si]
  453.     mov  al,PELPANNING+20h
  454.     mov  si,ax
  455.  
  456. ; VSync Handler stuff
  457.  
  458.     cmp  VSyncHandlerActive, TRUE
  459.     jne   @@NoVsyncHandler
  460.  
  461. @@WaitLast:
  462.     cmp   StartAddressFlag,0
  463.     jne   @@WaitLast
  464.     cli
  465.     mov  WaitingStartLow,bx
  466.     mov  WaitingStartHigh,cx
  467.     mov  WaitingPelPan,si
  468.     mov  StartAddressFlag,1
  469.     sti
  470.     jmp  short @@Return
  471.  
  472. ; End VSync Handler stuff
  473.  
  474. @@NoVsyncHandler:
  475.     mov  dx,InputStatus0
  476.  
  477. @@WaitDE:
  478.     in   al,dx
  479.     test al,01h
  480.     jnz  @@WaitDE
  481.     mov  dx,CrtcIndex
  482.     mov  ax,bx
  483.     cli
  484.     out  dx,ax
  485.     mov  ax,cx
  486.     out  dx,ax
  487.     sti
  488.     mov  dx,InputStatus0
  489.  
  490. @@WaitVS:
  491.     in   al,dx
  492.     test al,08h
  493.     jz @@WaitVS
  494.  
  495.     mov  dx,ACINDEX
  496.     mov  ax,si
  497.     cli
  498.     out  dx,al
  499.     mov  al,ah
  500.     out  dx,al
  501.     sti
  502.  
  503. @@Return:
  504.     mov  ErrorValue,OK
  505.     ret
  506.  
  507. XSetStartAddr ENDP
  508.  
  509. XHideSplitscreen PROC FAR
  510.  
  511.     cmp  SplitScrnActive,1
  512.     je   @@SplitScreenEnabled
  513.  
  514. @@error:
  515.     mov  ErrorValue,ERROR
  516.     jmp  @@Done
  517.  
  518. @@SplitScreenEnabled:
  519.     cmp  CurrXMode,4
  520.     jg   @@error
  521.     mov  bx,ScrnPhysicalHeight
  522.     mov  ax,ScrnLogicalHeight
  523.     sub  ax,bx
  524.     mov  MaxScrollY,ax
  525.     xor  ax,ax
  526.     mov  SplitScrnVisibleHeight,ax
  527.     or   DoubleScanFlag,0
  528.     jz   @@NotDoubleScanned
  529.     shl  bx,1
  530.     dec  bx
  531.  
  532. @@NotDoubleScanned:
  533.     cmp  VSyncHandlerActive,1
  534.     je   @@VSyncHandling
  535.     mov  dx,InputStatus0
  536.  
  537. @@WaitNotVsync:
  538.     in   al,dx
  539.     test al,08h
  540.     jnz  @@WaitNotVsync
  541.  
  542. @@WaitVsync:
  543.     in   al,dx
  544.     test al,08h
  545.     jz   @@WaitVsync
  546.     jmp  short @@SetRegs
  547. @@VSyncHandling:
  548.     cmp  StartAddressFlag,0
  549.     jne  @@VSyncHandling
  550.     mov  StartAddressFlag,1
  551. @@SetRegs:
  552.     cli
  553.     mov  dx,CrtcIndex
  554.     mov  ah,bl
  555.     mov  al,LINECOMPARE
  556.     out  dx,ax
  557.     mov  ah,bh
  558.     and  ah,1
  559.     shl  ah,4
  560.     mov  al,Overflow
  561.     out  dx,al
  562.     inc  dx
  563.     in   al,dx
  564.     and  al, not 10h
  565.     or   al,ah
  566.     out  dx,al
  567.     dec  dx
  568.     mov  ah,bh
  569.     and  ah,2
  570.     ror  ah,3
  571.     mov  al,MAXSCANLINE
  572.     out  dx,al
  573.     inc  dx
  574.     in   al,dx
  575.     and  al, not 40h
  576.     or   al,ah
  577.     out  dx,al
  578.     sti
  579.  
  580. @@done:
  581.     mov  ErrorValue,OK
  582.     ret
  583.  
  584. XHideSplitScreen ENDP
  585.  
  586. XShowSplitscreen PROC FAR
  587.  
  588.     cmp  SplitScrnActive,1
  589.     je   @@SplitScreenEnabled
  590.  
  591. @@error:
  592.     mov  ErrorValue,ERROR
  593.     jmp  @@Done
  594.  
  595. @@SplitScreenEnabled:
  596.     cmp  CurrXMode,4
  597.     jg   @@error
  598.     mov  bx,SplitScrnScanLine
  599.     mov  ax,ScrnLogicalHeight
  600.     sub  ax,bx
  601.     mov  MaxScrollY,ax
  602.     mov  ax,ScrnPhysicalHeight
  603.     sub  ax,bx
  604.     mov  SplitScrnVisibleHeight,ax
  605.     or   DoubleScanFlag,0
  606.     jz   @@NotDoubleScanned
  607.     shl  bx,1
  608.     dec  bx
  609.  
  610. @@NotDoubleScanned:
  611.     cmp  VsyncHandlerActive,1
  612.     je   @@VSyncHandling
  613.     mov  dx,InputStatus0
  614.  
  615. @@WaitNotVsync:
  616.     in   al,dx
  617.     test al,08h
  618.     jnz  @@WaitNotVsync
  619.  
  620. @@WaitVsync:
  621.     in   al,dx
  622.     test al,08h
  623.     jz   @@WaitVsync
  624.     jmp short @@SetRegs
  625. @@VSyncHandling:
  626.     cmp  StartAddressFlag,0
  627.     jne  @@VSyncHandling
  628.     mov  StartAddressFlag,1
  629. @@SetRegs:
  630.     cli
  631.     mov  dx,CrtcIndex
  632.     mov  ah,bl
  633.     mov  al,LINECOMPARE
  634.     out  dx,ax
  635.     mov  ah,bh
  636.     and  ah,1
  637.     shl  ah,4
  638.     mov  al,Overflow
  639.     out  dx,al
  640.     inc  dx
  641.     in   al,dx
  642.     and  al, not 10h
  643.     or   al,ah
  644.     out  dx,al
  645.     dec  dx
  646.     mov  ah,bh
  647.     and  ah,2
  648.     ror  ah,3
  649.     mov  al,MAXSCANLINE
  650.     out  dx,al
  651.     inc  dx
  652.     in   al,dx
  653.     and  al, not 40h
  654.     or   al,ah
  655.     out  dx,al
  656.     sti
  657.  
  658. @@Done:
  659.     mov  ErrorValue,0
  660.     ret
  661.  
  662. XShowSplitScreen ENDP
  663.  
  664. XAdjustSplitScreen PROC FAR Line : Word
  665.  
  666.     cmp  SplitScrnActive,1
  667.     je   @@SplitScreenEnabled
  668.  
  669. @@error:
  670.     mov  ErrorValue,ERROR
  671.     jmp  @@Done
  672.  
  673. @@SplitScreenEnabled:
  674.     cmp  CurrXMode,4
  675.     jg   @@error
  676.     mov  bx,Line
  677.     cmp  bx,SplitScrnScanLine
  678.     js   @@Done
  679.  
  680. @@ValidScanLine:
  681.     mov  ax,ScrnLogicalHeight
  682.     sub  ax,bx
  683.     mov  MaxScrollY,ax
  684.     mov  ax,ScrnPhysicalHeight
  685.     sub  ax,bx
  686.     mov  SplitScrnVisibleHeight,ax
  687.     or   DoubleScanFlag,0
  688.     jz   @@NotDoubleScanned
  689.     shl  bx,1
  690.     dec  bx
  691.  
  692. @@NotDoubleScanned:
  693.     cmp  VsyncHandlerActive,1
  694.     je   @@VsyncHandling
  695.  
  696.     mov  dx,InputStatus0
  697.  
  698. @@WaitNotVsync:
  699.     in   al,dx
  700.     test al,08h
  701.     jnz  @@WaitNotVsync
  702.  
  703. @@WaitVsync:
  704.     in   al,dx
  705.     test al,08h
  706.     jz   @@WaitVsync
  707.     jmp  short @@SetRegs
  708.  
  709. @@VsyncHandling:
  710.     cmp  StartAddressFlag,0
  711.     jne  @@VSyncHandling
  712.     mov  StartAddressFlag,2
  713. @@SetRegs:
  714.     cli
  715.     mov  dx,CrtcIndex
  716.     mov  ah,bl
  717.     mov  al,LINECOMPARE
  718.     out  dx,ax
  719.     mov  ah,bh
  720.     and  ah,1
  721.     shl  ah,4
  722.     mov  al,Overflow
  723.     out  dx,al
  724.     inc  dx
  725.     in   al,dx
  726.     and  al, not 10h
  727.     or   al,ah
  728.     out  dx,al
  729.     dec  dx
  730.     mov  ah,bh
  731.     and  ah,2
  732.     ror  ah,3
  733.     mov  al,MAXSCANLINE
  734.     out  dx,al
  735.     inc  dx
  736.     in   al,dx
  737.     and  al, not 40h
  738.     or   al,ah
  739.     out  dx,al
  740.     sti
  741.  
  742. @@Done:
  743.     mov  ErrorValue,OK
  744.     ret
  745.  
  746. XAdjustSplitScreen ENDP
  747.  
  748. XSetDoubleBuffer PROC FAR PageHeight : Word
  749.  
  750.     cmp   DoubleBufferActive,0
  751.     je    @@OkToContinue
  752. @error:
  753.     mov   ErrorValue,ERROR
  754.     ret
  755.  
  756. @@OkToContinue:
  757.     mov  VisiblePageIdx,0
  758.     mov  ax,ScrnLogicalHeight
  759.     shr  ax,1
  760.     mov  bx,PageHeight
  761.     cmp  ax,bx
  762.     js   @@InvalidHeight
  763.     mov  ax,bx
  764.  
  765. @@InvalidHeight:
  766.     mov   ScrnLogicalHeight,ax
  767.     cmp   ax,BottomClip
  768.     jle   @@BottomClipOK
  769.     mov   BottomClip,ax
  770. @@BottomClipOK:
  771.     mov   dx,ax
  772.     mul   ScrnLogicalByteWidth
  773.     mov   cx,ax
  774.     mov   bx,Page0Offs
  775.     mov   VisiblePageOffs,bx
  776.     add   ax,bx
  777.     mov   Page1Offs,ax
  778.     mov   HiddenPageOffs,ax
  779.     add   ax,cx
  780.     mov   NonVisualOffs,ax
  781.     mov   DoubleBufferActive,1
  782.     mov   ax,dx
  783.     sub   ax,ScrnPhysicalHeight
  784.     add   ax,SplitScrnVisibleHeight
  785.     mov   MaxScrollY,ax
  786.     mov   ax,dx
  787.     mov   ErrorValue,OK
  788.     ret
  789.  
  790. XSetDoubleBuffer ENDP
  791.  
  792. XSetTripleBuffer PROC FAR PageHeight : word
  793.  
  794.     cmp   DoubleBufferActive,0
  795.     jne   @@Error
  796.     cmp   TripleBufferActive,0
  797.     je    @@OkToContinue
  798.  
  799. @@Error:
  800.     mov   ErrorValue,ERROR
  801.     jmp @@Done
  802.  
  803. @@OkToContinue:
  804.     mov   VisiblePageIdx,0
  805.     mov   ax,ScrnLogicalHeight
  806.     mov   bx,3
  807.     xor   dx,dx
  808.     idiv  bx
  809.     mov   bx,PageHeight
  810.     cmp   ax,bx
  811.     js    @@InvalidHeight
  812.     mov   ax,bx
  813.  
  814. @@InvalidHeight:
  815.     mov   ScrnLogicalHeight,ax
  816.     cmp   ax,BottomClip
  817.     jle   @@BottomClipOK
  818.     mov   BottomClip,ax
  819.  
  820. @@BottomClipOK:
  821.     push  ax
  822.     mul   ScrnLogicalByteWidth
  823.     mov   cx,ax
  824.     mov   bx,Page0Offs
  825.     mov   VisiblePageOffs,bx
  826.     add   ax,bx
  827.     mov   Page1Offs,ax
  828.     mov   HiddenPageOffs,ax
  829.     add   ax,cx
  830.     mov   Page2Offs,ax
  831.     mov   WaitingPageOffs,ax
  832.     add   ax,cx
  833.     mov   NonVisualOffs,ax
  834.     mov   TripleBufferActive,1
  835.     pop   ax
  836.     sub   ax,ScrnPhysicalHeight
  837.     add   ax,SplitScrnVisibleHeight
  838.     mov   MaxScrollY,ax
  839.     mov   ax,dx
  840.     mov   ErrorValue,OK
  841.  
  842. @@Done:
  843.     ret
  844.  
  845. XSetTripleBuffer ENDP
  846.  
  847. XPageFlip PROC FAR X : Word, Y : Word
  848.  
  849.     mov  si,x
  850.     mov  ax,ScrnLogicalByteWidth
  851.     mov  cx,y
  852.     mul  cx
  853.     cmp  DoubleBufferActive,1
  854.     je   @@DoubleBuffer
  855.     cmp  TripleBufferActive,1
  856.     jne  @@PageFlipEntry1
  857.     mov  bx,HiddenPageOffs
  858.     xchg bx,VisiblePageOffs
  859.     xchg bx,WaitingPageOffs
  860.     mov  HiddenPageOffs,bx
  861.     mov  bx, VisiblePageIdx
  862.     inc  bx
  863.     cmp  bx,3
  864.     jne  @@IdxOk
  865.     xor  bx,bx
  866. @@IdxOk:
  867.     mov  VisiblePageIdx,bx
  868.     jmp  @@PageFlipEntry2
  869.  
  870. @@DoubleBuffer:
  871.     mov  bx,HiddenPageOffs
  872.     xchg bx,VisiblePageOffs
  873.     xchg HiddenPageOffs,bx
  874.     xor  VisiblePageIdx,01h
  875.     jmp  @@PageFlipEntry2
  876.  
  877. @@PageFlipEntry1:
  878.     add  ax,Page0Offs
  879.     jmp  @@AddColumn
  880.  
  881. @@PageFlipEntry2:
  882.     mov  PhysicalStartPixelX,si
  883.     mov  PhysicalStartY,cx
  884.  
  885. @@PageResolution:
  886.     add  ax,VisiblePageOffs
  887.  
  888. @@AddColumn:
  889.     mov  cx,si
  890.     shr  cx,2
  891.     mov  PhysicalStartByteX,cx
  892.     add  ax,cx
  893.     mov  bh,al
  894.     mov  ch,ah
  895.  
  896. @@StartAddrEntry:
  897.     mov  bl,ADDRLOW
  898.     mov  cl,ADDRHIGH
  899.     and  si,0003h
  900.     mov  ah,byte ptr PelPanMask[si]
  901.     mov  al,PELPANNING+20h
  902.     mov  si,ax
  903. ; VSync Handler stuff
  904.  
  905.     cmp  VSyncHandlerActive, TRUE
  906.     jne   @@NoVsyncHandler
  907.  
  908. @@WaitLast:
  909.     cmp   StartAddressFlag,0
  910.     jne   @@WaitLast
  911.     cli
  912.     mov  WaitingStartLow,bx
  913.     mov  WaitingStartHigh,cx
  914.     mov  WaitingPelPan,si
  915.     mov  StartAddressFlag,1
  916.     sti
  917.     jmp  short @@Return
  918.  
  919. ; End VSync Handler stuff
  920.  
  921. @@NoVsyncHandler:
  922.     mov  dx,InputStatus0
  923.  
  924. @@WaitDE:
  925.     in   al,dx
  926.     test al,01h
  927.     jnz  @@WaitDE
  928.     mov  dx,CrtcIndex
  929.     mov  ax,bx
  930.     cli
  931.     out  dx,ax
  932.     mov  ax,cx
  933.     out  dx,ax
  934.     sti
  935.     mov  dx,InputStatus0
  936.  
  937. @@WaitVS:
  938.     in   al,dx
  939.     test al,08h
  940.     jz @@WaitVS
  941.  
  942.     mov  dx,ACINDEX
  943.     mov  ax,si
  944.     cli
  945.     out  dx,al
  946.     mov  al,ah
  947.     out  dx,al
  948.     sti
  949.  
  950. @@Return:
  951.     mov  ErrorValue,OK
  952.     ret
  953.  
  954. XPageFlip ENDP
  955.  
  956. XSetClipRect PROC FAR Left : Word, Top : Word, Right : Word, Bottom : Word
  957.  
  958.     mov   ax,left
  959.     mov   bx,right
  960.     cmp   bx,ax
  961.     jns   @@CorrectXOrder
  962.     xchg  bx,ax
  963. @@CorrectXOrder:
  964.     mov   LeftClip,ax
  965.     mov   RightClip,bx
  966.     mov   ax,top
  967.     mov   bx,bottom
  968.     cmp   bx,ax
  969.     jns   @@CorrectYOrder
  970.     xchg  bx,ax
  971. @@CorrectYOrder:
  972.     mov   TopClip,ax
  973.     mov   BottomClip,bx
  974.     ret
  975.  
  976. XSetClipRect ENDP
  977.  
  978. XTextMode PROC FAR
  979.  
  980.     mov   ax,03h
  981.     int   10h
  982.     ret
  983.  
  984. XTextMode ENDP
  985.  
  986. XWaitVSync PROC FAR
  987.  
  988.     mov   dx,InputStatus0
  989. @@WaitNotVsync:
  990.     in    al,dx
  991.     test  al,08h
  992.     jnz   @@WaitNotVsync
  993.  
  994. @@WaitVsync:
  995.     in    al,dx
  996.     test  al,08h
  997.     jz    @@WaitVsync
  998.     ret
  999.  
  1000. XWaitVSync ENDP
  1001.  
  1002. XLine PROC FAR x1 : Word, y1 : Word, x2 : Word, y2 : Word, Color : Word,\
  1003.                              PgOffs : Word
  1004. LOCAL vertincr : Word, incr1 : Word, incr2 : Word, routine : Word
  1005.  
  1006.     mov ax,ScreenSeg
  1007.     mov es,ax
  1008.  
  1009.     mov dx,ScIndex
  1010.     mov si,ScrnLogicalByteWidth
  1011.     mov cx,x2
  1012.     sub cx,x1
  1013.     jz  @@VertLine
  1014.     jns @@L01
  1015.  
  1016.     neg cx
  1017.  
  1018.     mov bx,x2
  1019.     xchg  bx,x1
  1020.     mov x2,bx
  1021.  
  1022.     mov bx,y2
  1023.     xchg  bx,y1
  1024.     mov y2,bx
  1025.  
  1026. @@L01:
  1027.     mov bx,y2
  1028.     sub bx,y1
  1029.     jnz @@skip
  1030.     jmp @@HorizLine
  1031.  
  1032. @@skip:
  1033.     jns @@L03
  1034.     neg bx
  1035.     neg si
  1036.  
  1037. @@L03:
  1038.     mov vertincr,si
  1039.     mov routine,offset @@LoSlopeLine
  1040.     cmp bx,cx
  1041.     jle @@L04
  1042.     mov routine,offset @@HiSlopeLine
  1043.     xchg  bx,cx
  1044.  
  1045.  
  1046. @@L04:
  1047.     shl bx,1
  1048.     mov incr1,bx
  1049.     sub bx,cx
  1050.     mov si,bx
  1051.     sub bx,cx
  1052.     mov incr2,bx
  1053.  
  1054.     push  cx
  1055.     mov ax,y1
  1056.     mov bx,x1
  1057.     mov cl,bl
  1058.     push dx
  1059.     mov dx,ScrnLogicalByteWidth
  1060.     mul dx
  1061.     pop dx
  1062.     shr bx,2
  1063.     add bx,ax
  1064.     add bx,PgOffs
  1065.     and cl,3
  1066.     mov di,bx
  1067.     mov al,1
  1068.     shl al,cl
  1069.     mov ah,al
  1070.     shl al,4
  1071.     add ah,al
  1072.     mov bl,ah
  1073.     pop cx
  1074.     inc cx
  1075.     jmp routine
  1076.  
  1077. @@VertLine:
  1078.     mov ax,y1
  1079.     mov bx,y2
  1080.     mov cx,bx
  1081.     sub cx,ax
  1082.     jge @@L31
  1083.     neg cx
  1084.     mov ax,bx
  1085.  
  1086. @@L31:
  1087.     inc cx
  1088.     mov bx,x1
  1089.     push  cx
  1090.     mov cl,bl
  1091.     push dx
  1092.     mov dx,ScrnLogicalByteWidth
  1093.     mul dx
  1094.     pop dx
  1095.     shr bx,2
  1096.     add bx,ax
  1097.     add bx,PgOffs
  1098.     and cl,3
  1099.  
  1100.     mov ah,1
  1101.     shl ah,cl
  1102.     mov al,MapMask
  1103.     out dx,ax
  1104.     pop cx
  1105.     mov ax, word ptr [Color]
  1106.  
  1107. @@L32:
  1108.     mov es:[bx],al
  1109.     add bx,si
  1110.     loop  @@L32
  1111.     jmp @@Lexit
  1112.  
  1113. @@HorizLine:
  1114.     push  ds
  1115.  
  1116.     mov ax,y1
  1117.     mov bx,x1
  1118.     mov cl,bl
  1119.     push dx
  1120.     mov dx,ScrnLogicalByteWidth
  1121.     mul dx
  1122.     pop dx
  1123.     shr bx,2
  1124.     add bx,ax
  1125.     add bx,PgOffs
  1126.     and cl,3
  1127.  
  1128.     mov di,bx
  1129.     mov dl,00fh
  1130.     shl dl,cl
  1131.  
  1132.     mov cx,x2
  1133.     and cl,3
  1134.     mov dh,00eh
  1135.     shl dh,cl
  1136.     not dh
  1137.  
  1138.     mov ax,x2
  1139.     mov bx,x1
  1140.  
  1141.     shr ax,2
  1142.     shr bx,2
  1143.     mov cx,ax
  1144.     sub cx,bx
  1145.  
  1146.     mov ax,dx
  1147.     mov dx,SCINDEX
  1148.     mov bx, [Color]
  1149.     or  cx,cx
  1150.     jnz @@L42
  1151.     and ah,al
  1152.     jmp @@L44
  1153.  
  1154. @@L42:
  1155.     push ax
  1156.     mov ah,al
  1157.     mov al,MAPMASK
  1158.     out dx,ax
  1159.     mov al,bl
  1160.     stosb
  1161.     dec cx
  1162.  
  1163. @@L43:
  1164.     mov ah,0Fh
  1165.     mov al,MAPMASK
  1166.     out dx,ax
  1167.     mov al,bl
  1168.     rep stosb
  1169.     pop     ax
  1170.  
  1171. @@L44:
  1172.     mov al,MAPMASK
  1173.     out dx, ax
  1174.     mov     byte ptr es:[di],bl
  1175.     pop ds
  1176.     jmp @@Lexit
  1177.  
  1178.  
  1179. @@LoSlopeLine:
  1180.     mov al,MAPMASK
  1181.     mov bh,byte ptr [Color]
  1182. @@L10:
  1183.     mov ah,bl
  1184.  
  1185. @@L11:
  1186.     or  ah,bl
  1187.     rol bl,1
  1188.     jc  @@L14
  1189.  
  1190.     or  si,si
  1191.     jns @@L12
  1192.     add si,incr1
  1193.     loop  @@L11
  1194.  
  1195.     out dx,ax
  1196.     mov es:[di],bh
  1197.     jmp @@Lexit
  1198.  
  1199. @@L12:
  1200.     add si,incr2
  1201.     out dx,ax
  1202.     mov es:[di],bh
  1203.     add di,vertincr
  1204.     loop  @@L10
  1205.     jmp @@Lexit
  1206.  
  1207. @@L14:
  1208.     out dx,ax
  1209.     mov es:[di],bh
  1210.     inc di
  1211.     or  si,si
  1212.     jns @@L15
  1213.     add si,incr1
  1214.     loop  @@L10
  1215.     jmp @@Lexit
  1216.  
  1217. @@L15:
  1218.     add si,incr2
  1219.     add di,vertincr
  1220.     loop  @@L10
  1221.     jmp @@Lexit
  1222.  
  1223. @@HiSlopeLine:
  1224.     mov bx,vertincr
  1225.     mov al,MAPMASK
  1226. @@L21:
  1227.     out dx,ax
  1228.     push  ax
  1229.     mov ax,Color
  1230.     mov es:[di],al
  1231.     pop ax
  1232.     add di,bx
  1233.  
  1234. @@L22:
  1235.     or  si,si
  1236.     jns @@L23
  1237.  
  1238.     add si,incr1
  1239.     loop  @@L21
  1240.     jmp @@Lexit
  1241.  
  1242. @@L23:
  1243.     add si,incr2
  1244.     rol ah,1
  1245.     adc di,0
  1246. @@lx21: loop  @@L21
  1247.  
  1248.  
  1249. @@Lexit:
  1250.     ret
  1251.  
  1252. XLine ENDP
  1253.  
  1254. XPutPix PROC FAR X : Word, Y : Word, PgOfs : Word, Color : Word
  1255.  
  1256.     mov  ax,ScrnLogicalByteWidth
  1257.     mul  Y
  1258.     mov  bx,X
  1259.     shr  bx,2
  1260.     add  bx,ax
  1261.     add  bx,PgOfs
  1262.     mov  ax,SCREENSEG
  1263.     mov  es,ax
  1264.  
  1265.     mov  cl,byte ptr X
  1266.     and  cl,011b
  1267.     mov  ax,0100h + MAPMASK
  1268.     shl  ah,cl
  1269.     mov  dx,SCINDEX
  1270.     out  dx,ax
  1271.     mov  al,byte ptr Color
  1272.     mov  es:[bx],al
  1273.     ret
  1274.  
  1275. XPutPix ENDP
  1276.  
  1277. XGetPix PROC FAR x : Word, y : Word, PageBase : Word
  1278.  
  1279.     mov  ax,ScrnLogicalByteWidth
  1280.     mul  Y
  1281.     mov  bx,X
  1282.     shr  bx,1
  1283.     shr  bx,1
  1284.     add  bx,ax
  1285.     add  bx,PageBase
  1286.     mov  ax,SCREENSEG
  1287.     mov  es,ax
  1288.     mov  ah,byte ptr X
  1289.     and  ah,011b
  1290.     mov  al,READMAP
  1291.     mov  dx,GCINDEX
  1292.     out  dx,ax
  1293.     mov  al,es:[bx]
  1294.     sub  ah,ah
  1295.     ret
  1296.  
  1297. XGetPix ENDP
  1298.  
  1299. XRectFill PROC FAR StartX : Word, StartY : Word, EndX : Word, EndY : Word,\
  1300.                         PageBase : Word, Color : Word
  1301.  
  1302.     push bp
  1303.     cld
  1304.     mov  ax,ScrnLogicalByteWidth
  1305.     mul  StartY
  1306.     mov  di,StartX
  1307.     shr  di,1
  1308.     shr  di,1
  1309.     add  di,ax
  1310.     add  di,PageBase
  1311.     mov  ax,SCREENSEG
  1312.     mov  es,ax
  1313.     mov  dx,SCINDEX
  1314.     mov  al,MAPMASK
  1315.     out  dx,al
  1316.     inc  dx
  1317.     mov  si,StartX
  1318.     and  si,0003h
  1319.     mov  bh,byte ptr LeftClipPlaneMask[si]
  1320.     mov  si,EndX
  1321.     and  si,0003h
  1322.     mov  bl,byte ptr RightClipPlaneMask[si]
  1323.     mov  cx,EndX
  1324.     mov  si,StartX
  1325.     cmp  cx,si
  1326.     jle  @@FillDone
  1327.     dec  cx
  1328.     and  si,not 011b
  1329.     sub  cx,si
  1330.     shr  cx,1
  1331.     shr  cx,1
  1332.     jnz  @@MasksSet
  1333.     and  bh,bl
  1334.  
  1335. @@MasksSet:
  1336.     mov  si,EndY
  1337.     sub  si,StartY
  1338.     jle  @@FillDone
  1339.     mov  ah,byte ptr Color
  1340.     mov  bp,ScrnLogicalByteWidth
  1341.     sub  bp,cx
  1342.     dec  bp
  1343. @@FillRowsLoop:
  1344.     push cx
  1345.     mov  al,bh
  1346.     out  dx,al
  1347.     mov  al,ah
  1348.     stosb
  1349.     dec  cx
  1350.     js   @@FillLoopBottom
  1351.     jz   @@DoRightEdge
  1352.     mov  al,00fh
  1353.     out  dx,al
  1354.     mov  al,ah
  1355.     rep  stosb
  1356. @@DoRightEdge:
  1357.     mov  al,bl
  1358.     out  dx,al
  1359.     mov  al,ah
  1360.     stosb
  1361. @@FillLoopBottom:
  1362.     add  di,bp
  1363.     pop  cx
  1364.     dec  si
  1365.     jnz  @@FillRowsLoop
  1366. @@FillDone:
  1367.     pop bp
  1368.     ret
  1369.  
  1370. XRectFill ENDP
  1371.  
  1372.  
  1373.  
  1374. XRectPattern PROC FAR StartX : Word, StartY : Word, EndX : Word, EndY : Word,\
  1375.                                             PageBase : Word, Pattern : DWord
  1376. LOCAL NextScanOffset : Word, RectAddrWidth : Word, Height : word
  1377.  
  1378.     push ds
  1379.     cld
  1380.     mov  ax,SCREENSEG
  1381.     mov  es,ax
  1382.     lds  si,Pattern
  1383.     mov  di,PATTERNBUFFER
  1384.     mov  dx,SCINDEX
  1385.     mov  al,MAPMASK
  1386.     out  dx,al
  1387.     inc  dx
  1388.     mov  cx,4
  1389. @@DownloadPatternLoop:
  1390.     mov  al,1
  1391.     out  dx,al
  1392.     movsb
  1393.     dec  di
  1394.     mov  al,2
  1395.     out  dx,al
  1396.     movsb
  1397.     dec  di
  1398.     mov  al,4
  1399.     out  dx,al
  1400.     movsb
  1401.     dec  di
  1402.     mov  al,8
  1403.     out  dx,al
  1404.     movsb
  1405.     loop @@DownloadPatternLoop
  1406.     pop  ds
  1407.  
  1408.     mov  dx,GCINDEX
  1409.     mov  ax,00000h+BITMASK
  1410.     out  dx,ax
  1411.  
  1412.     mov  ax,StartY
  1413.     mov  si,ax
  1414.     and  si,011b
  1415.     add  si,PATTERNBUFFER
  1416.  
  1417.     mov  dx,ScrnLogicalByteWidth
  1418.     mul  dx
  1419.     mov  di,StartX
  1420.     mov  bx,di
  1421.     shr  di,1
  1422.     shr  di,1
  1423.     add  di,ax
  1424.     add  di,PageBase
  1425.  
  1426.     and  bx,0003h
  1427.     mov  ah,byte ptr LeftClipPlaneMask[bx]
  1428.     mov  bx,EndX
  1429.     and  bx,0003h
  1430.     mov  al,byte ptr RightClipPlaneMask[bx]
  1431.     mov  bx,ax
  1432.  
  1433.     mov  cx,EndX
  1434.     mov  ax,StartX
  1435.     cmp  cx,ax
  1436.     jle  @@FillDone
  1437.     dec  cx
  1438.     and  ax,not 011b
  1439.     sub  cx,ax
  1440.     shr  cx,1
  1441.     shr  cx,1
  1442.     jnz  @@MasksSet
  1443.     and  bh,bl
  1444.  
  1445. @@MasksSet:
  1446.     mov  ax,EndY
  1447.     sub  ax,StartY
  1448.     jle  @@FillDone
  1449.     mov  Height,ax
  1450.     mov  ax,ScrnLogicalByteWidth
  1451.     sub  ax,cx
  1452.     dec  ax
  1453.     mov  NextScanOffset,ax
  1454.     mov  RectAddrWidth,cx
  1455.     mov  dx,SCINDEX+1
  1456.  
  1457. @@FillRowsLoop:
  1458.     mov  cx,RectAddrWidth
  1459.     mov  al,es:[si]
  1460.     inc  si
  1461.     jnz  @@NoWrap
  1462.     sub  si,4
  1463. @@NoWrap:
  1464.     mov  al,bh
  1465.     out  dx,al
  1466.     stosb
  1467.  
  1468.     dec  cx
  1469.     js   @@FillLoopBottom
  1470.     jz   @@DoRightEdge
  1471.     mov  al,00fh
  1472.     out  dx,al
  1473.     rep  stosb
  1474.  
  1475. @@DoRightEdge:
  1476.     mov  al,bl
  1477.     out  dx,al
  1478.     stosb
  1479.  
  1480. @@FillLoopBottom:
  1481.     add  di,NextScanOffset
  1482.     dec  word ptr Height
  1483.     jnz  @@FillRowsLoop
  1484. @@FillDone:
  1485.     mov  dx,GCINDEX+1
  1486.     mov  al,0ffh
  1487.     out  dx,al
  1488.     ret
  1489.  
  1490. XRectPattern ENDP
  1491.  
  1492. XCpVidPage PROC FAR SourceOffs : Word, DestOffs : Word
  1493.  
  1494. LOCAL ScreenSize : Word
  1495.  
  1496.     push    ds
  1497.     cld
  1498.     mov     ax, ScrnLogicalByteWidth
  1499.     mov     bx, ScrnLogicalHeight
  1500.     mul     bx
  1501.     mov     ScreenSize, ax
  1502.     mov     ax, SCREENSEG
  1503.     mov     ds, ax
  1504.     mov     es, ax
  1505.  
  1506. @@PlaneLoop:
  1507.  
  1508.     mov     si, SourceOffs
  1509.     mov     di, DestOffs
  1510.  
  1511.     mov     ax,BITMASK
  1512.     mov     dx,GCINDEX
  1513.     out     dx,ax
  1514.     mov     dx,SCINDEX+1
  1515.     mov     al, 00fh
  1516.     out     dx, al
  1517.  
  1518.     mov     cx,ScreenSize
  1519.  
  1520.     rep     movsb
  1521.  
  1522.     mov     dx,GCINDEX+1
  1523.     mov     al,0ffh
  1524.     out     dx,al
  1525.     pop     ds
  1526.     ret
  1527.  
  1528. XCpVidPage ENDP
  1529.  
  1530. XCpVidRect PROC FAR SrcStartX : Word, SrcStartY : Word, SrcEndX : Word,\
  1531.                                         SrcEndY : Word, DestStartX : Word, DestStartY : Word,\
  1532.                                         SrcPageBase : Word, DestPageBase : Word,\
  1533.                                         SrcBitmapW : Word, DestBitmapW : Word
  1534. LOCAL SrcNextOffs : Word, DestNextOffs : Word, RectAddrW : Word, Height : Word
  1535.  
  1536.     push    ds
  1537.     cld
  1538.     mov     dx,GCINDEX
  1539.     mov     ax,BITMASK
  1540.     out     dx,ax
  1541.     mov     ax,SCREENSEG
  1542.     mov     es,ax
  1543.     mov     ax,DestBitmapW
  1544.     shr     ax,2
  1545.     mul     DestStartY
  1546.     mov     di,DestStartX
  1547.     shr     di,2
  1548.     add     di,ax
  1549.     add     di,DestPageBase
  1550.     mov     ax,SrcBitmapW
  1551.     shr     ax,2
  1552.     mul     SrcStartY
  1553.     mov     si,SrcStartX
  1554.     mov     bx,si
  1555.     shr     si,2
  1556.     add     si,ax
  1557.     add     si,SrcPageBase
  1558.     and     bx,0003h
  1559.     mov     ah,byte ptr LeftClipPlaneMask[bx]
  1560.     mov     bx,SrcEndX
  1561.     and     bx,0003h
  1562.     mov     al,byte ptr RightClipPlaneMask[bx]
  1563.     mov     bx,ax
  1564.     mov     cx,SrcEndX
  1565.     mov     ax,SrcStartX
  1566.     cmp     cx,ax
  1567.     jle     @@CopyDone
  1568.     dec     cx
  1569.     and     ax,not 011b
  1570.     sub     cx,ax
  1571.     shr     cx,2
  1572.     jnz     @@MasksSet
  1573.     and     bh,bl
  1574. @@MasksSet:
  1575.     mov     ax,SrcEndY
  1576.     sub     ax,SrcStartY
  1577.     jle     @@CopyDone
  1578.     mov     Height,ax
  1579.     mov     ax,DestBitmapW
  1580.     shr     ax,2
  1581.     sub     ax,cx
  1582.     dec     ax
  1583.     mov     DestNextOffs,ax
  1584.     mov     ax,SrcBitmapW
  1585.     shr     ax,2
  1586.     sub     ax,cx
  1587.     dec     ax
  1588.     mov     SrcNextOffs,ax
  1589.     mov     RectAddrW,cx
  1590.     mov     dx,SCINDEX+1
  1591.     mov     ax,es
  1592.     mov     ds,ax
  1593. @@CopyRowsLoop:
  1594.     mov     cx,RectAddrW
  1595.     mov     al,bh
  1596.     out     dx,al
  1597.     movsb
  1598.     dec     cx
  1599.     js      @@CopyLoopBottom
  1600.     jz      @@DoRightEdge
  1601.     mov     al,00fh
  1602.     out     dx,al
  1603.     rep     movsb
  1604. @@DoRightEdge:
  1605.     mov     al,bl
  1606.     out     dx,al
  1607.     movsb
  1608. @@CopyLoopBottom:
  1609.     add     si,SrcNextOffs
  1610.     add     di,DestNextOffs
  1611.     dec     word ptr Height
  1612.     jnz     @@CopyRowsLoop
  1613. @@CopyDone:
  1614.     mov     dx,GCINDEX+1
  1615.     mov     al,0ffh
  1616.     out     dx,al
  1617.     pop     ds
  1618.     ret
  1619.  
  1620. XCpVidRect ENDP
  1621.  
  1622. XShiftRect PROC FAR SrcLeft : Word, SrcTop : Word, SrcRight : Word,\
  1623.                                         SrcBottom : Word, DestLeft : Word, DestTop : Word,\
  1624.                                         ScreenOffs : Word
  1625. LOCAL widthtemp : Word
  1626.  
  1627.     push ds
  1628.     mov si, SrcLeft
  1629.     shr si, 2
  1630.  
  1631.     mov di, DestLeft
  1632.     shr di, 2
  1633.  
  1634.     mov bx, SrcRight
  1635.     add bx, 3
  1636.     shr bx, 2
  1637.     sub bx, si
  1638.     mov ax, bx
  1639.     inc bx
  1640.     mov widthtemp, bx
  1641.  
  1642.     cld
  1643.  
  1644.     cmp si, di
  1645.     jge @@MovingLeft
  1646.  
  1647.     add si, ax
  1648.     add di, ax
  1649.     neg bx
  1650.     std
  1651.  
  1652. @@MovingLeft:
  1653.  
  1654.     mov cx, ScrnLogicalByteWidth
  1655.     mov ax, SrcTop
  1656.     mov dx, DestTop
  1657.     cmp ax, dx
  1658.     jge @@MovingUp
  1659.  
  1660.     mov ax, SrcBottom
  1661.     add dx, ax
  1662.     sub dx, SrcTop
  1663.     neg cx
  1664.  
  1665. @@MovingUp:
  1666.     push dx
  1667.     mul ScrnLogicalByteWidth
  1668.     add si, ax
  1669.     pop ax
  1670.     mul ScrnLogicalByteWidth
  1671.     add di, ax
  1672.  
  1673.     sub cx, bx
  1674.  
  1675.     add si, ScreenOffs
  1676.     add di, ScreenOffs
  1677.  
  1678.     mov dx, SCINDEX
  1679.     mov ax, 00f02h
  1680.     out dx, ax
  1681.  
  1682.     mov dx, GCINDEX
  1683.     mov ax, BITMASK
  1684.     out dx, ax
  1685.  
  1686.     mov ax, SCREENSEG
  1687.     mov es, ax
  1688.     mov ds, ax
  1689.  
  1690.     mov ah, byte ptr widthtemp
  1691.  
  1692.     mov bx, SrcBottom
  1693.     sub bx, SrcTop
  1694.  
  1695.     mov dx, cx
  1696.  
  1697.     xor ch, ch
  1698.  
  1699. @@LineLoop:
  1700.     mov cl, ah
  1701.     rep movsb
  1702.  
  1703.     add si, dx
  1704.     add di, dx
  1705.  
  1706.     dec bx
  1707.     jge @@LineLoop
  1708.  
  1709.     mov dx, GCINDEX + 1
  1710.     mov al, 0ffh
  1711.     out dx, al
  1712.     pop ds
  1713.     ret
  1714.  
  1715. XShiftRect ENDP
  1716.  
  1717. XCircle PROC FAR Left : Word, Top : Word, Diameter : Word, Color : Word,\
  1718.                                  ScreenOffs : Word
  1719. LOCAL offset0 : Word, offset1 : Word, offset2 : Word, offset3 : Word,\
  1720.             offset4 : Word, offset5 : Word, offset6 : Word, offset7 : Word,\
  1721.             mask0n1 : Word, mask2n3 : Word, mask4n5 : Word, mask6n7 : Word,\
  1722.             shrunkradius : Word, diametereven : Word, Aerror : Word
  1723.  
  1724.     push ds
  1725.     mov di, ScrnLogicalByteWidth
  1726.     xor dx, dx
  1727.     mov ax, Diameter
  1728.     dec ax
  1729.     shr ax, 1
  1730.     adc dx, 0
  1731.     mov shrunkradius, ax
  1732.     mov diametereven, dx
  1733.     add ax, Top
  1734.     mul di
  1735.     add ax, ScreenOffs
  1736.  
  1737.     mov bx, Left
  1738.     mov cx, bx
  1739.     mov si, bx
  1740.     shr si, 2
  1741.     add si, ax
  1742.     mov offset6, si
  1743.     and bx, 3
  1744.     mov bl, byte ptr ColumnMask[bx]
  1745.     mov mask6n7, bx
  1746.  
  1747.     add cx, Diameter
  1748.     dec cx
  1749.     mov bx, cx
  1750.     shr cx, 2
  1751.     add cx, ax
  1752.     mov offset2, cx
  1753.     and bx, 3
  1754.     mov bl, byte ptr ColumnMask[bx]
  1755.     mov mask2n3, bx
  1756.  
  1757.     cmp diametereven, 1
  1758.     jne @@MiddlePlotsOverlap
  1759.     add si, di
  1760.     add cx, di
  1761. @@MiddlePlotsOverlap:
  1762.     mov offset7, si
  1763.     mov offset3, cx
  1764.  
  1765.     mov bx, Left
  1766.     add bx, shrunkradius
  1767.  
  1768.     mov ax, Top
  1769.     mul di
  1770.     add ax, ScreenOffs
  1771.     mov si, ax
  1772.  
  1773.     mov ax, Diameter
  1774.     dec ax
  1775.     mul di
  1776.     add ax, si
  1777.  
  1778.     mov di, bx
  1779.     shr di, 2
  1780.     add si, di
  1781.     mov offset4, si
  1782.     add di, ax
  1783.     mov offset5, di
  1784.     and bx, 3
  1785.     mov bl, byte ptr ColumnMask[bx]
  1786.     mov mask4n5, bx
  1787.  
  1788.     cmp diametereven, 1
  1789.     jne @@TopAndBottomPlotsOverlap
  1790.     rol bl, 1
  1791.     jnc @@TopAndBottomPlotsOverlap
  1792.     inc si
  1793.     inc di
  1794. @@TopAndBottomPlotsOverlap:
  1795.     mov offset0, si
  1796.     mov offset1, di
  1797.     mov mask0n1, bx
  1798.     mov bx, ScrnLogicalByteWidth
  1799.  
  1800.     mov dx, SCREENSEG
  1801.     mov ds, dx
  1802.  
  1803.     mov dx, SCINDEX
  1804.     mov al, MAPMASK
  1805.     out dx, al
  1806.     inc dx
  1807.  
  1808.     mov si, Diameter
  1809.     inc si
  1810.  
  1811.     mov cx, si
  1812.     neg cx
  1813.     add cx, 2
  1814.     mov Aerror, cx
  1815.  
  1816.     xor cx, cx
  1817.     mov ah, byte ptr Color
  1818.     jmp @@CircleCalc
  1819.  
  1820. @@NoAdvance:
  1821.     mov al, byte ptr mask0n1
  1822.     out dx, al
  1823.     mov di, offset0
  1824.     mov [di], ah
  1825.     rol al, 1
  1826.     mov byte ptr mask0n1, al
  1827.     adc di, 0
  1828.     mov offset0, di
  1829.     mov di, offset1
  1830.     mov [di], ah
  1831.     ror al, 1
  1832.     adc di, 0
  1833.     mov offset1, di
  1834.  
  1835.     mov al, byte ptr mask2n3
  1836.     out dx, al
  1837.     mov di, offset2
  1838.     mov [di], ah
  1839.     sub di, bx
  1840.     mov offset2, di
  1841.     mov di, offset3
  1842.     mov [di], ah
  1843.     add di, bx
  1844.     mov offset3, di
  1845.  
  1846.     mov al, byte ptr mask4n5
  1847.     out dx, al
  1848.     mov di, offset4
  1849.     mov [di], ah
  1850.     ror al, 1
  1851.     mov byte ptr mask4n5, al
  1852.     sbb di, 0
  1853.     mov offset4, di
  1854.     mov di, offset5
  1855.     mov [di], ah
  1856.     rol al, 1
  1857.     sbb di, 0
  1858.     mov offset5, di
  1859.  
  1860.     mov al, byte ptr mask6n7
  1861.     out dx, al
  1862.     mov di, offset6
  1863.     mov [di], ah
  1864.     sub di, bx
  1865.     mov offset6, di
  1866.     mov di, offset7
  1867.     mov [di], ah
  1868.     add di, bx
  1869.     mov offset7, di
  1870.  
  1871.     jmp @@CircleCalc
  1872.  
  1873. @@Advance:
  1874.     mov al, byte ptr mask0n1
  1875.     out dx, al
  1876.     mov di, offset0
  1877.     mov [di], ah
  1878.     rol al, 1
  1879.     mov byte ptr mask0n1, al
  1880.     adc di, bx
  1881.     mov offset0, di
  1882.     mov di, offset1
  1883.     mov [di], ah
  1884.     ror al, 1
  1885.     adc di, 0
  1886.     sub di, bx
  1887.     mov offset1, di
  1888.  
  1889.     mov al, byte ptr mask2n3
  1890.     out dx, al
  1891.     mov di, offset2
  1892.     mov [di], ah
  1893.     ror al, 1
  1894.     mov byte ptr mask2n3, al
  1895.     sbb di, bx
  1896.     mov offset2, di
  1897.     mov di, offset3
  1898.     mov [di], ah
  1899.     rol al, 1
  1900.     sbb di, 0
  1901.     add di, bx
  1902.     mov offset3, di
  1903.  
  1904.     mov al, byte ptr mask4n5
  1905.     out dx, al
  1906.     mov di, offset4
  1907.     mov [di], ah
  1908.     ror al, 1
  1909.     mov byte ptr mask4n5, al
  1910.     sbb di, 0
  1911.     add di, bx
  1912.     mov offset4, di
  1913.     mov di, offset5
  1914.     mov [di], ah
  1915.     rol al, 1
  1916.     sbb di, bx
  1917.     mov offset5, di
  1918.  
  1919.     mov al, byte ptr mask6n7
  1920.     out dx, al
  1921.     mov di, offset6
  1922.     mov [di], ah
  1923.     rol al, 1
  1924.     mov byte ptr mask6n7, al
  1925.     adc di, 0
  1926.     sub di, bx
  1927.     mov offset6, di
  1928.     mov di, offset7
  1929.     mov [di], ah
  1930.     ror al, 1
  1931.     adc di, bx
  1932.     mov offset7, di
  1933.  
  1934. @@CircleCalc:
  1935.     add cx, 2
  1936.     mov di, Aerror
  1937.     add di, cx
  1938.     inc di
  1939.     jl @@CircleNoError
  1940.     cmp cx, si
  1941.     ja @@FleeFlyFlowFum
  1942.     sub si, 2
  1943.     sub di, si
  1944.     mov Aerror, di
  1945.     jmp @@Advance
  1946. @@CircleNoError:
  1947.     mov Aerror, di
  1948.     jmp @@NoAdvance
  1949.  
  1950. @@FleeFlyFlowFum:
  1951.     pop ds
  1952.     ret
  1953.  
  1954. XCircle ENDP
  1955.  
  1956. XFilledCircle PROC FAR Left : Word, Top : Word, Diameter : Word,\
  1957.                                              Color : Word, ScreenOffs : Word
  1958. LOCAL offset0 : Word, offset1 : Word, offset2 : Word, offset3 : Word,\
  1959.             offset4 : Word, offset5 : Word, offset6 : Word, offset7 : Word,\
  1960.             mask0n1 : Word, mask2n3 : Word, mask4n5 : Word, mask6n7 : Word,\
  1961.             shrunkradius : Word, diametereven : Word, Aerror : Word,\
  1962.             jumpvector : Word
  1963.  
  1964.     cld
  1965.     mov di, ScrnLogicalByteWidth
  1966.     xor dx, dx
  1967.     mov ax, Diameter
  1968.     dec ax
  1969.     shr ax, 1
  1970.     adc dx, 0
  1971.     mov shrunkradius, ax
  1972.     mov diametereven, dx
  1973.     add ax, Top
  1974.     mul di
  1975.     add ax, ScreenOffs
  1976.  
  1977.     mov bx, Left
  1978.     mov cx, bx
  1979.     mov si, bx
  1980.     shr si, 2
  1981.     add si, ax
  1982.     mov offset6, si
  1983.     and bx, 3
  1984.     mov bl, byte ptr ColumnMask[bx]
  1985.     mov mask6n7, bx
  1986.  
  1987.     add cx, Diameter
  1988.     dec cx
  1989.     mov bx, cx
  1990.     shr cx, 2
  1991.     add cx, ax
  1992.     mov offset2, cx
  1993.     and bx, 3
  1994.     mov bl, byte ptr ColumnMask[bx]
  1995.     mov mask2n3, bx
  1996.  
  1997.     cmp diametereven, 1
  1998.     jne @@MiddlePlotsOverlap
  1999.     add si, di
  2000.     add cx, di
  2001. @@MiddlePlotsOverlap:
  2002.     mov offset7, si
  2003.     mov offset3, cx
  2004.  
  2005.     mov bx, Left
  2006.     add bx, shrunkradius
  2007.  
  2008.     mov ax, Top
  2009.     mul di
  2010.     add ax, ScreenOffs
  2011.     mov si, ax
  2012.  
  2013.     mov ax, Diameter
  2014.     dec ax
  2015.     mul di
  2016.     add ax, si
  2017.  
  2018.     mov di, bx
  2019.     shr di, 2
  2020.     add si, di
  2021.     mov offset4, si
  2022.     add di, ax
  2023.     mov offset5, di
  2024.     and bx, 3
  2025.     mov bl, byte ptr ColumnMask[bx]
  2026.     mov mask4n5, bx
  2027.  
  2028.     cmp diametereven, 1
  2029.     jne @@TopAndBottomPlotsOverlap
  2030.     rol bl, 1
  2031.     jnc @@TopAndBottomPlotsOverlap
  2032.     inc si
  2033.     inc di
  2034. @@TopAndBottomPlotsOverlap:
  2035.     mov offset0, si
  2036.     mov offset1, di
  2037.     mov mask0n1, bx
  2038.  
  2039.     mov bx, ScrnLogicalByteWidth
  2040.  
  2041.     mov dx, SCREENSEG
  2042.     mov es, dx
  2043.  
  2044.     mov dx, SCINDEX
  2045.     mov al, MAPMASK
  2046.     out dx, al
  2047.     inc dx
  2048.     mov si, Diameter
  2049.     inc si
  2050.  
  2051.     mov cx, si
  2052.     neg cx
  2053.     add cx, 2
  2054.     mov Aerror, cx
  2055.     xor cx, cx
  2056.     mov ah, byte ptr Color
  2057.     jmp @@FilledCircleCalc
  2058. @@PlotLines:
  2059.     push cx
  2060.  
  2061.     mov di, mask0n1
  2062.     and di, 0000fh
  2063.     mov al, byte ptr RightMaskTable[di]
  2064.     mov di, offset0
  2065.     cmp di, offset4
  2066.     jne @@PlotXMajorNontrivial
  2067.     mov di, mask4n5
  2068.     and di, 0000fh
  2069.     and al, byte ptr LeftMaskTable[di]
  2070.     out dx, al
  2071.     mov di, offset4
  2072.     mov es:[di], ah
  2073.     mov di, offset5
  2074.     mov es:[di], ah
  2075.     jmp @@PlotYMajor
  2076. @@PlotXMajorNontrivial:
  2077.     out dx, al
  2078.     mov es:[di], ah
  2079.     mov di, offset1
  2080.     mov es:[di], ah
  2081.  
  2082.     mov di, mask4n5
  2083.     and di, 0000fh
  2084.     mov al, byte ptr LeftMaskTable[di]
  2085.     out dx, al
  2086.     mov di, offset4
  2087.     mov es:[di], ah
  2088.     mov di, offset5
  2089.     mov es:[di], ah
  2090.  
  2091.     mov al, 0ffh
  2092.     out dx, al
  2093.     mov al, ah
  2094.     inc di
  2095.     mov cx, offset1
  2096.     sub cx, di
  2097.     push cx
  2098.     shr cx, 1
  2099.     rep stosw
  2100.     adc cx, 0
  2101.     rep stosb
  2102.  
  2103.     mov di, offset4
  2104.     inc di
  2105.     pop cx
  2106.     shr cx, 1
  2107.     rep stosw
  2108.     adc cx, 0
  2109.     rep stosb
  2110.  
  2111. @@PlotYMajor:
  2112.     mov di, mask2n3
  2113.     and di, 0000fh
  2114.     mov al, byte ptr RightMaskTable[di]
  2115.     mov di, offset2
  2116.     cmp di, offset6
  2117.     jne @@PlotYMajorNontrivial
  2118.     mov di, mask6n7
  2119.     and di, 0000fh
  2120.     and al, byte ptr LeftMaskTable[di]
  2121.     out dx, al
  2122.     mov di, offset6
  2123.     mov es:[di], ah
  2124.     mov di, offset7
  2125.     mov es:[di], ah
  2126.     jmp @@ClimaxOfPlot
  2127. @@PlotYMajorNontrivial:
  2128.     out dx, al
  2129.     mov es:[di], ah
  2130.     mov di, offset3
  2131.     mov es:[di], ah
  2132.  
  2133.     mov di, mask6n7
  2134.     and di, 0000fh
  2135.     mov al, byte ptr LeftMaskTable[di]
  2136.     out dx, al
  2137.     mov di, offset6
  2138.     mov es:[di], ah
  2139.     mov di, offset7
  2140.     mov es:[di], ah
  2141.  
  2142.     mov al, 0ffh
  2143.     out dx, al
  2144.     mov al, ah
  2145.  
  2146.     inc di
  2147.     mov cx, offset3
  2148.     sub cx, di
  2149.     push cx
  2150.     shr cx, 1
  2151.     rep stosw
  2152.     adc cx, 0
  2153.     rep stosb
  2154.  
  2155.     mov di, offset6
  2156.     inc di
  2157.     pop cx
  2158.     shr cx, 1
  2159.     rep stosw
  2160.     adc cx, 0
  2161.     rep stosb
  2162.  
  2163. @@ClimaxOfPlot:
  2164.     pop cx
  2165.     jmp [jumpvector]
  2166.  
  2167.  
  2168. @@NoAdvance:
  2169.     mov al, byte ptr mask0n1
  2170.     mov di, offset0
  2171.     rol al, 1
  2172.     mov byte ptr mask0n1, al
  2173.     adc di, 0
  2174.     mov offset0, di
  2175.     mov di, offset1
  2176.     ror al, 1
  2177.     adc di, 0
  2178.     mov offset1, di
  2179.  
  2180.     mov al, byte ptr mask4n5
  2181.     mov di, offset4
  2182.     ror al, 1
  2183.     mov byte ptr mask4n5, al
  2184.     sbb di, 0
  2185.     mov offset4, di
  2186.     mov di, offset5
  2187.     rol al, 1
  2188.     sbb di, 0
  2189.     mov offset5, di
  2190.  
  2191.     mov al, byte ptr mask2n3
  2192.     mov di, offset2
  2193.     sub di, bx
  2194.     mov offset2, di
  2195.     mov di, offset3
  2196.     add di, bx
  2197.     mov offset3, di
  2198.  
  2199.     mov al, byte ptr mask6n7
  2200.     mov di, offset6
  2201.     sub di, bx
  2202.     mov offset6, di
  2203.     mov di, offset7
  2204.     add di, bx
  2205.     mov offset7, di
  2206.  
  2207.     jmp @@FilledCircleCalc
  2208.  
  2209. @@Advance:
  2210.     mov al, byte ptr mask0n1
  2211.     mov di, offset0
  2212.     rol al, 1
  2213.     mov byte ptr mask0n1, al
  2214.     adc di, bx
  2215.     mov offset0, di
  2216.     mov di, offset1
  2217.     ror al, 1
  2218.     adc di, 0
  2219.     sub di, bx
  2220.     mov offset1, di
  2221.  
  2222.     mov al, byte ptr mask2n3
  2223.     mov di, offset2
  2224.     ror al, 1
  2225.     mov byte ptr mask2n3, al
  2226.     sbb di, bx
  2227.     mov offset2, di
  2228.     mov di, offset3
  2229.     rol al, 1
  2230.     sbb di, 0
  2231.     add di, bx
  2232.     mov offset3, di
  2233.  
  2234.     mov al, byte ptr mask4n5
  2235.     mov di, offset4
  2236.     ror al, 1
  2237.     mov byte ptr mask4n5, al
  2238.     sbb di, 0
  2239.     add di, bx
  2240.     mov offset4, di
  2241.     mov di, offset5
  2242.     rol al, 1
  2243.     sbb di, bx
  2244.     mov offset5, di
  2245.  
  2246.     mov al, byte ptr mask6n7
  2247.     mov di, offset6
  2248.     rol al, 1
  2249.     mov byte ptr mask6n7, al
  2250.     adc di, 0
  2251.     sub di, bx
  2252.     mov offset6, di
  2253.     mov di, offset7
  2254.     ror al, 1
  2255.     adc di, bx
  2256.     mov offset7, di
  2257.  
  2258. @@FilledCircleCalc:
  2259.     add cx, 2
  2260.     mov di, Aerror
  2261.     add di, cx
  2262.     inc di
  2263.     jl @@FilledCircleNoError
  2264.     cmp cx, si
  2265.     ja @@FleeFlyFlowFum
  2266.     sub si, 2
  2267.     sub di, si
  2268.     mov Aerror, di
  2269.     mov jumpvector, offset @@Advance
  2270.     jmp @@PlotLines
  2271. @@FilledCircleNoError:
  2272.     mov Aerror, di
  2273.     mov jumpvector, offset @@NoAdvance
  2274.     jmp @@PlotLines
  2275.  
  2276. @@FleeFlyFlowFum:
  2277.     ret
  2278.  
  2279. XFilledCircle ENDP
  2280.  
  2281. XGetPalStruc PROC FAR PalBuff : DWord, NumColors : Word, StartColor : Word
  2282.  
  2283.     cld
  2284.     les  di,PalBuff
  2285.     mov  si,StartColor
  2286.     mov  ax,si
  2287.     stosb
  2288.     mov  dx,NumColors
  2289.     mov  al,dl
  2290.     stosb
  2291.     mov  cx,dx
  2292.     cld
  2293.     mov  dx,INPUTSTATUS0
  2294. @@WaitNotVsync:
  2295.     in   al,dx
  2296.     test al,08h
  2297.     jnz  @@WaitNotVsync
  2298. @@WaitVsync:
  2299.     in   al,dx
  2300.     test al,08h
  2301.     jz   @@WaitVsync
  2302.     mov  ax, si
  2303.     mov  dx, DACREADINDEX
  2304.     cli
  2305.     out  dx, al
  2306.     mov  dx, DACDATA
  2307.     mov  bx, cx
  2308.     shl  bx, 1
  2309.     add  cx, bx
  2310.     rep  insb
  2311.     sti
  2312.     ret
  2313.  
  2314. XGetPalStruc ENDP
  2315.  
  2316. XGetPalRaw PROC FAR PalBuff : DWord, NumColors : Word, StartColor : Word
  2317.  
  2318.     les  di,PalBuff
  2319.     mov  si,StartColor
  2320.     mov  cx,NumColors
  2321. @@ReadPalEntry:
  2322.     cld
  2323.     mov     dx,INPUTSTATUS0
  2324. @@WaitNotVsync:
  2325.     in      al,dx
  2326.     test    al,08h
  2327.     jnz     @@WaitNotVsync
  2328. @@WaitVsync:
  2329.     in      al,dx
  2330.     test    al,08h
  2331.     jz      @@WaitVsync
  2332.     mov  ax,si
  2333.     mov  dx,DACREADINDEX
  2334.     cli
  2335.     out  dx,al
  2336.     mov  dx,DACDATA
  2337.     mov  bx,cx
  2338.     shl  bx,1
  2339.     add  cx,bx
  2340.     rep  insb
  2341.     sti
  2342.     ret
  2343.  
  2344. XGetPalRaw ENDP
  2345.  
  2346.  
  2347. XPutPalStruc PROC FAR CompPalBuff : DWord
  2348.  
  2349.     push ds
  2350.     cld
  2351.     lds  si,CompPalBuff
  2352.     lodsb
  2353.     mov  ah,0
  2354.     mov  bx,ax
  2355.     lodsb
  2356.     mov  ah,0
  2357.     mov  cx,ax
  2358.  
  2359.     mov  ax, @data
  2360.     mov  es, ax
  2361.     cmp  es:[VSyncHandlerActive], TRUE
  2362.     jne  @@NoVSyncHandler
  2363.  
  2364. @@WaitForLast:
  2365.     cmp  es:[VsyncPaletteCount],0
  2366.     jne  @@WaitForLast
  2367.     push cx
  2368.     push es
  2369.     mov  di, offset VsyncPaletteBuffer
  2370.     mov  ax,3
  2371.     mul  cx
  2372.     mov  cx,ax
  2373.     rep  movsb
  2374.     pop  ds
  2375.     pop  cx
  2376.     mov  VsyncPaletteStart,bx
  2377.     mov  VsyncPaletteCount,cx
  2378.     jmp  short @@Done
  2379.  
  2380. @@NoVsyncHandler:
  2381.     or   cx,cx
  2382.     jz   @@Done
  2383.     ;cli
  2384.     cld
  2385.     mov  dx,INPUTSTATUS0
  2386. @@WaitNotVsync:
  2387.     in   al,dx
  2388.     test al,08h
  2389.     jnz  @@WaitNotVsync
  2390. @@WaitVsync:
  2391.     in   al,dx
  2392.     test al,08h
  2393.     jz   @@WaitVsync
  2394.     mov  ax,bx
  2395.     mov  bx,60
  2396. @@SetLoop:
  2397.     mov  dx,DACWRITEINDEX
  2398.     out  dx,al
  2399.     mov  dx,DACDATA
  2400.     outsb
  2401.     outsb
  2402.     outsb
  2403.     inc  al
  2404.     dec  bx
  2405.     js   @@testvsync
  2406.     loop @@SetLoop
  2407.     jmp  @@Done
  2408. @@testvsync:
  2409.     mov     dx,INPUTSTATUS0
  2410.     push    ax
  2411. @@Wait:
  2412.     in      al,dx
  2413.     test    al,08h
  2414.     jz      @@Wait
  2415.     pop     ax
  2416.     mov     bx,60
  2417.     loop @@SetLoop
  2418. @@Done:
  2419.     sti
  2420.     pop  ds
  2421.     ret
  2422.  
  2423. XPutPalStruc ENDP
  2424.  
  2425. XTransposePalStruc PROC FAR CompPalBuff : DWord, StartColor : Word
  2426.  
  2427.     push ds
  2428.     cld
  2429.     lds  si,CompPalBuff
  2430.     mov  bx,StartColor
  2431.     mov  [si],bl
  2432.     inc  si
  2433.     lodsb
  2434.     mov  ah,0
  2435.     mov cx,ax
  2436.  
  2437.     mov  ax, @data
  2438.     mov  es, ax
  2439.     cmp  es:[VSyncHandlerActive], TRUE
  2440.     jne  @@NoVSyncHandler
  2441.  
  2442. @@WaitForLast:
  2443.     cmp  es:[VsyncPaletteCount],0
  2444.     jne  @@WaitForLast
  2445.     push cx
  2446.     push es
  2447.     mov  di, offset VsyncPaletteBuffer
  2448.     mov  ax,3
  2449.     mul  cx
  2450.     mov  cx,ax
  2451.     rep  movsb
  2452.     pop  ds
  2453.     pop  cx
  2454.     mov  VsyncPaletteStart,bx
  2455.     mov  VsyncPaletteCount,cx
  2456.     jmp  short @@Done
  2457.  
  2458. @@NoVsyncHandler:
  2459.     or   cx,cx
  2460.     jz   @@Done
  2461.     ;cli
  2462.     cld
  2463.     mov  dx,INPUTSTATUS0
  2464. @@WaitNotVsync:
  2465.     in   al,dx
  2466.     test al,08h
  2467.     jnz  @@WaitNotVsync
  2468. @@WaitVsync:
  2469.     in   al,dx
  2470.     test al,08h
  2471.     jz   @@WaitVsync
  2472.     mov  ax,bx
  2473.     mov  bx,60
  2474. @@SetLoop:
  2475.     mov  dx,DACWRITEINDEX
  2476.     out  dx,al
  2477.     mov  dx,DACDATA
  2478.     outsb
  2479.     outsb
  2480.     outsb
  2481.     inc  al
  2482.     dec  bx
  2483.     js   @@testvsync
  2484.     loop @@SetLoop
  2485.     jmp  @@Done
  2486. @@testvsync:
  2487.     mov     dx,INPUTSTATUS0
  2488.     push    ax
  2489. @@Wait:
  2490.     in      al,dx
  2491.     test    al,08h
  2492.     jz      @@Wait
  2493.     pop     ax
  2494.     mov     bx,60
  2495.     loop @@SetLoop
  2496. @@Done:
  2497.     sti
  2498.     pop  ds
  2499.     ret
  2500.  
  2501. XTransposePalStruc ENDP
  2502.  
  2503.  
  2504. XPutPalRaw PROC FAR PalBuff : DWord, NumColors : Word, StartColor : Word
  2505.  
  2506.     push ds
  2507.     mov  cx,NumColors
  2508.     mov  bx,StartColor
  2509.     lds  si,PalBuff
  2510.  
  2511. @@WritePalEntry:
  2512.     mov  ax, @data
  2513.     mov  es, ax
  2514.     cmp  es:[VSyncHandlerActive], TRUE
  2515.     jne  @@NoVSyncHandler
  2516.  
  2517. @@WaitForLast:
  2518.     cmp  es:[VsyncPaletteCount],0
  2519.     jne  @@WaitForLast
  2520.     push cx
  2521.     push es
  2522.     mov  di, offset VsyncPaletteBuffer
  2523.     mov  ax,3
  2524.     mul  cx
  2525.     mov  cx,ax
  2526.     rep  movsb
  2527.     pop  ds
  2528.     pop  cx
  2529.     mov  VsyncPaletteStart,bx
  2530.     mov  VsyncPaletteCount,cx
  2531.     jmp  short @@Done
  2532.  
  2533. @@NoVsyncHandler:
  2534.     or   cx,cx
  2535.     jz   @@Done
  2536.     ;cli
  2537.     cld
  2538.     mov  dx,INPUTSTATUS0
  2539. @@WaitNotVsync:
  2540.     in   al,dx
  2541.     test al,08h
  2542.     jnz  @@WaitNotVsync
  2543. @@WaitVsync:
  2544.     in   al,dx
  2545.     test al,08h
  2546.     jz   @@WaitVsync
  2547.     mov  ax,bx
  2548.     mov  bx,60
  2549. @@SetLoop:
  2550.     mov  dx,DACWRITEINDEX
  2551.     out  dx,al
  2552.     mov  dx,DACDATA
  2553.     outsb
  2554.     outsb
  2555.     outsb
  2556.     inc  al
  2557.     dec  bx
  2558.     js   @@testvsync
  2559.     loop @@SetLoop
  2560.     jmp  @@Done
  2561. @@testvsync:
  2562.     mov     dx,INPUTSTATUS0
  2563.     push    ax
  2564. @@Wait:
  2565.     in      al,dx
  2566.     test    al,08h
  2567.     jz      @@Wait
  2568.     pop     ax
  2569.     mov     bx,60
  2570.     loop @@SetLoop
  2571. @@Done:
  2572.     sti
  2573.     pop  ds
  2574.     ret
  2575.  
  2576. XPutPalRaw ENDP
  2577.  
  2578.  
  2579. XSetRGB PROC FAR ColorIndex : Byte, R : Byte, G : Byte, B : Byte
  2580.  
  2581.     mov  al,ColorIndex
  2582.     mov  dx,DACWRITEINDEX
  2583.     out  dx,al
  2584.     mov  dx,DACDATA
  2585.     mov  al,R
  2586.     out  dx,al
  2587.     mov  al,G
  2588.     out  dx,al
  2589.     mov  al,B
  2590.     out  dx,al
  2591.     ret
  2592.  
  2593. XSetRGB ENDP
  2594.  
  2595. XRotPalStruc PROC FAR PalBuff : DWord, Direction : Word
  2596.  
  2597.     push ds
  2598.     cld
  2599.     lds  si,PalBuff
  2600.     lodsw
  2601.     xor  ch,ch
  2602.     mov  cl,ah
  2603.     mov  ax,ds
  2604.     mov  es,ax
  2605.     dec  cx
  2606.     mov  bx,cx
  2607.     shl  bx,1
  2608.     add  cx,bx
  2609.     cmp  Direction,0
  2610.     jne  @@forward
  2611.     std
  2612.     add  si,cx
  2613.     add  si,2
  2614. @@forward:
  2615.     mov  ax,si
  2616.     mov  di,ax
  2617.     lodsb
  2618.     mov  dl,al
  2619.     lodsb
  2620.     mov  dh,al
  2621.     lodsb
  2622.     mov  bl,al
  2623.     rep  movsb
  2624.     mov  al,dl
  2625.     stosb
  2626.     mov  al,dh
  2627.     stosb
  2628.     mov  al,bl
  2629.     stosb
  2630.     pop  ds
  2631.     ret
  2632.  
  2633. XRotPalStruc ENDP
  2634.  
  2635. XRotPalRaw PROC FAR PalBuff : DWord, Direction : Word, NumColors : Word
  2636.  
  2637.     push ds
  2638.     cld
  2639.     mov  cx,NumColors
  2640.     lds  si,PalBuff
  2641. @@RotatePalEntry:
  2642.     mov  ax,ds
  2643.     mov  es,ax
  2644.     dec  cx
  2645.     mov  bx,cx
  2646.     shl  bx,1
  2647.     add  cx,bx
  2648.     cmp  Direction,0
  2649.     jne  @@forward
  2650.     std
  2651.     add  si,cx
  2652.     add  si,2
  2653. @@forward:
  2654.     mov  ax,si
  2655.     mov  di,ax
  2656.     lodsb
  2657.     mov  dl,al
  2658.     lodsb
  2659.     mov  dh,al
  2660.     lodsb
  2661.     mov  bl,al
  2662.     rep  movsb
  2663.     mov  al,dl
  2664.     stosb
  2665.     mov  al,dh
  2666.     stosb
  2667.     mov  al,bl
  2668.     stosb
  2669.     pop  ds
  2670.     ret
  2671.  
  2672. XRotPalRaw ENDP
  2673.  
  2674. XCpContrastPalStruc PROC FAR PalSrcBuff : DWord, PalDestBuff : DWord,\
  2675.                                                          Intensity : Byte
  2676.  
  2677.     push ds
  2678.     cld
  2679.     mov  bh,0ffh
  2680.     sub  bh,Intensity
  2681.     and  bh,07fh
  2682.     lds  si,PalSrcBuff
  2683.     les  di,PalDestBuff
  2684.     lodsw
  2685.     stosw
  2686.     xor  ch,ch
  2687.     mov  cl,ah
  2688.     mov  dx,0
  2689. @@MainLoop:
  2690.     lodsw
  2691.     sub  al,bh
  2692.     jns  @@DecrementOKR
  2693.     xor  al,al
  2694. @@DecrementOKR:
  2695.     sub  ah,bh
  2696.     jns  @@DecrementOKG
  2697.     xor  ah,ah
  2698. @@DecrementOKG:
  2699.     or   dx,ax
  2700.     or   dl,ah
  2701.     stosw
  2702.     lodsb
  2703.     sub  al,bh
  2704.     jns  @@DecrementOKB
  2705.     xor  al,al
  2706. @@DecrementOKB:
  2707.     or   dl,al
  2708.     stosb
  2709.     loop @@MainLoop
  2710.     mov  ax,dx
  2711.     pop  ds
  2712.     ret
  2713.  
  2714. XCpContrastPalStruc ENDP
  2715.  
  2716.  
  2717. XPutContrastPalStruc PROC FAR CompPalBuff : DWord, Intensity : Byte
  2718.  
  2719.     push    ds
  2720.     cld
  2721.     mov     bh,0ffh
  2722.     sub     bh,Intensity
  2723.     and     bh,07fh
  2724.     mov     di,40
  2725.     lds     si,CompPalBuff
  2726.     lodsb
  2727.     mov     bl,al
  2728.     lodsb
  2729.     mov     ah,0
  2730.     mov     cx,ax
  2731.     or      cx,cx
  2732.     jz      @@Done
  2733.  
  2734.     mov     dx,INPUTSTATUS0
  2735. @@WaitNotVsync:
  2736.     in      al,dx
  2737.     test    al,08h
  2738.     jnz     @@WaitNotVsync
  2739. @@WaitVsync:
  2740.     in      al,dx
  2741.     test    al,08h
  2742.     jz      @@WaitVsync
  2743.  
  2744. @@MainLoop:
  2745.     mov  al,bl
  2746.     mov  dx,DACWRITEINDEX
  2747.     out  dx,al
  2748.     inc  dx
  2749.     lodsb
  2750.     sub  al,bh
  2751.     jns  @@DecrementOKR
  2752.     xor  al,al
  2753. @@DecrementOKR:
  2754.     out  dx,al
  2755.  
  2756.     lodsb
  2757.     sub  al,bh
  2758.     jns  @@DecrementOKG
  2759.     xor  al,al
  2760. @@DecrementOKG:
  2761.     out  dx,al
  2762.  
  2763.     lodsb
  2764.     sub  al,bh
  2765.     jns  @@DecrementOKB
  2766.     xor  al,al
  2767. @@DecrementOKB:
  2768.     out  dx,al
  2769.  
  2770.     inc  bl
  2771.     dec  di
  2772.     js   @@testvsync
  2773.     loop @@MainLoop
  2774.     jmp  @@Done
  2775.  
  2776.  
  2777. @@testvsync:
  2778.     mov     dx,INPUTSTATUS0
  2779.     push    ax
  2780. @@Wait:
  2781.     in      al,dx
  2782.     test    al,08h
  2783.     jz      @@Wait
  2784.     pop     ax
  2785.     mov     di,40
  2786.     loop    @@MainLoop
  2787. @@Done:
  2788.     ;sti
  2789.     pop  ds
  2790.     ret
  2791.  
  2792. XPutContrastPalStruc ENDP
  2793.  
  2794. XTextInit PROC FAR
  2795.  
  2796.     push bp
  2797.     mov  FontDriverActive,1
  2798.     mov  ax,1130h
  2799.     push ax
  2800.     mov  bh,3
  2801.     int  10h
  2802.     mov  word ptr F8x8Ptr,bp
  2803. IFDEF _DPMI_
  2804.     mov  ax, __C000H
  2805.     mov  es, ax
  2806. ENDIF
  2807.     mov  word ptr F8x8Ptr+2,es
  2808.  
  2809.     mov  word ptr FontPtr,bp
  2810.     mov  word ptr FontPtr+2,es
  2811.  
  2812.     pop  ax
  2813.     mov  bh,2
  2814.     int  10h
  2815.     mov  word ptr F8x14Ptr,bp
  2816. IFDEF _DPMI_
  2817.     mov  ax, __C000H
  2818.     mov  es, ax
  2819. ENDIF
  2820.     mov  word ptr F8x14Ptr+2,es
  2821.  
  2822.  
  2823.     mov  al,8
  2824.     mov  CharHeight,al
  2825.     mov  CharWidth ,al
  2826.  
  2827.     mov  dx,offset MirrorTable
  2828.     mov  MirrorTableOffs,dx
  2829.     pop  bp
  2830.     ret
  2831.  
  2832. XTextInit ENDP
  2833.  
  2834.  
  2835. XSetFont PROC FAR FontID : Word
  2836.  
  2837.     xor  dx,dx
  2838.     mov  cx,FontID
  2839.     cmp  cx,2
  2840.  
  2841.     jne  @@notuserfont
  2842.     mov  ax,word ptr UserFontPtr
  2843.     mov  word ptr FontPtr,ax
  2844.  
  2845.     mov  ax,word ptr UserFontPtr+2
  2846.     mov  word ptr FontPtr+2,ax
  2847.  
  2848.     mov  al,UserChHeight
  2849.     mov  CharHeight,al
  2850.  
  2851.     mov  al,UserChWidth
  2852.     mov  CharWidth,al
  2853.  
  2854.     mov  al,UserFirstCh
  2855.     mov  FirstChar,al
  2856.     jmp  @@done
  2857.  
  2858. @@notuserfont:
  2859.  
  2860.     mov  dx,offset MirrorTable
  2861.     mov  CharWidth,8
  2862.     mov  FirstChar,0
  2863.     cmp  cx,1
  2864.     jne  @@not8x14font
  2865.  
  2866.     mov  ax,word ptr F8x14Ptr
  2867.     mov  word ptr FontPtr,ax
  2868.  
  2869.     mov  ax,word ptr F8x14Ptr+2
  2870.     mov  word ptr FontPtr+2,ax
  2871.  
  2872.     mov  CharHeight,14
  2873.     jmp  @@done
  2874.  
  2875. @@not8x14font:
  2876.     mov  ax,word ptr F8x8Ptr
  2877.     mov  word ptr FontPtr,ax
  2878.  
  2879.     mov  ax,word ptr F8x8Ptr+2
  2880.     mov  word ptr FontPtr+2,ax
  2881.  
  2882.     mov  CharHeight,8
  2883.  
  2884. @@done:
  2885.     mov  MirrorTableOffs,dx
  2886.     ret
  2887.  
  2888. XSetFont ENDP
  2889.  
  2890. XRegisterUserFont PROC FAR FontToRegister : DWord
  2891.  
  2892.     mov  ax,word ptr FontToRegister
  2893.     mov  bx,word ptr FontToRegister+2
  2894.     add  ax,4
  2895.     mov  word ptr UserFontPtr,ax
  2896.     mov  word ptr UserFontPtr+2,bx
  2897.  
  2898.     push ds
  2899.     lds  si,FontToRegister
  2900.     lodsw
  2901.     mov  bx,ax
  2902.     lodsw
  2903.     pop  ds
  2904.     mov  UserChHeight,al
  2905.     mov  UserChWidth,ah
  2906.     mov  UserFirstCh,bl
  2907.     ret
  2908.  
  2909. XRegisterUserFont ENDP
  2910.  
  2911. XGetCharWidth PROC FAR ACh : Byte
  2912.  
  2913.     xor  ah,ah
  2914.     mov  al,CharWidth
  2915.     or   al,al
  2916.     jz   @@NotFixed
  2917.     jmp  @@Done
  2918.  
  2919. @@NotFixed:
  2920.     push si
  2921.     mov  al,CharHeight
  2922.     mov  bx,ax
  2923.     inc  al
  2924.     mov  dl, ACh
  2925.     sub  dl,FirstChar
  2926.     mul  dl
  2927.     add  ax,bx
  2928.     les  si,dword ptr FontPtr
  2929.     add  si,ax
  2930.     xor  ah,ah
  2931.     mov  al,byte ptr es:[si]
  2932. @@Done:
  2933.     ret
  2934.  
  2935. XGetCharWidth ENDP
  2936.  
  2937. XCharPut PROC FAR Chr : Byte, X : Word, Y : Word, ScrnOffs : Word, Color : Word
  2938. LOCAL ScreenInc : Word, Hold : Word
  2939.  
  2940.     push ds
  2941.     cld
  2942.     mov  ax,ScrnLogicalByteWidth
  2943.     mov  bx,ax
  2944.     sub  bx,3
  2945.     mov  ScreenInc,bx
  2946.     mul  Y
  2947.     mov  di,X
  2948.     mov  cx,di
  2949.     shr  di,2
  2950.     add  di,ax
  2951.     add  di,ScrnOffs
  2952.  
  2953.     mov  ax,SCREENSEG
  2954.     mov  es,ax
  2955.  
  2956.     and  cx,3
  2957.  
  2958.     mov  bx,MirrorTableOffs
  2959.     mov  al,CharHeight
  2960.     xor  ah,ah
  2961.     mov  ch,al
  2962.  
  2963.     cmp  CharWidth,0
  2964.     jne  @@NoWidthByte
  2965.     inc  al
  2966.  
  2967. @@NoWidthByte:
  2968.     mov  dl,Chr
  2969.     sub  dl,FirstChar
  2970.     mul  dl
  2971.     lds  si,dword ptr FontPtr
  2972.     add  si,ax
  2973.  
  2974.     mov  dx,SCINDEX
  2975.  
  2976. @@MainLoop:
  2977.  
  2978.     lodsb
  2979.     or   al,al
  2980.     jz   @@NoCharPixels
  2981.  
  2982.     or   bx,bx
  2983.     jz   @@DontMirror
  2984.     push ds
  2985.     mov  dx,seg @data
  2986.     mov  ds,dx
  2987.     xlat
  2988.     pop  ds
  2989.     mov  dx,SCINDEX
  2990.  
  2991. @@DontMirror:
  2992.     xor  ah,ah
  2993.     shl  ax,cl
  2994.     mov  Hold,ax
  2995.  
  2996.     mov  ah,al
  2997.     and  ah,0fh
  2998.     jnz  @@p1
  2999.     inc  di
  3000.     jmp  @@SecondNibble
  3001.  
  3002. @@p1:
  3003.     mov  al,MAPMASK
  3004.     out  dx,ax
  3005.     mov  al,byte ptr Color
  3006.     stosb
  3007.  
  3008. @@SecondNibble:
  3009.     mov  ax,Hold
  3010.     shl  ax,4
  3011.     and  ah,0fh
  3012.     jnz  @@p2
  3013.     inc  di
  3014.     jmp  @@ThirdNibble
  3015.  
  3016. @@p2:
  3017.     mov  al,MAPMASK
  3018.     out  dx,ax
  3019.     mov  al,byte ptr Color
  3020.     stosb
  3021.  
  3022. @@ThirdNibble:
  3023.     mov  ax,Hold
  3024.     and  ah,0fh
  3025.     jnz  @@p3
  3026.     inc  di
  3027.     jmp  @@NextCharRow
  3028.  
  3029. @@p3:
  3030.     mov  al,MAPMASK
  3031.     out  dx,ax
  3032.     mov  al,byte ptr Color
  3033.     stosb
  3034.  
  3035. @@NextCharRow:
  3036.     add  di,ScreenInc
  3037.     dec  ch
  3038.     jnz  @@MainLoop
  3039.  
  3040. @@done:
  3041.     pop  es
  3042.     mov  ah,0
  3043.     mov  al,es:CharWidth
  3044.     or   al,al
  3045.     jnz  @@FixedSpacing
  3046.     lodsb
  3047. @@FixedSpacing:
  3048.  
  3049.     mov  bx,es
  3050.     mov  ds,bx
  3051.     jmp @@TheEnd
  3052.  
  3053.  
  3054. @@NoCharPixels:
  3055.     add  di,3
  3056.     add  di,ScreenInc
  3057.     dec  ch
  3058.     jnz  @@MainLoop
  3059.     jmp  @@done
  3060.  
  3061. @@TheEnd:
  3062.     ret
  3063. XCharPut ENDP
  3064.  
  3065. HLineClipR PROC NEAR
  3066.  
  3067.     push    di
  3068.     cmp     dx,ax
  3069.     jl      @@Invisible
  3070.  
  3071.     cmp     cx,TopClip
  3072.     jl      @@Invisible
  3073.  
  3074.     cmp     cx,BottomClip
  3075.     jg      @@Invisible
  3076.  
  3077.     mov     di,RightClip
  3078.     sal     di,2
  3079.     cmp     ax,di
  3080.     jg      @@Invisible
  3081.     cmp     dx,di
  3082.     jle     @@ClipLeft
  3083.     mov     dx,di
  3084.  
  3085. @@ClipLeft:
  3086.     mov     di,LeftClip
  3087.     sal     di,2
  3088.     cmp     dx,di
  3089.     jl      @@Invisible
  3090.     cmp     ax,di
  3091.     jge     @@DoLine
  3092.     mov     ax,di
  3093.     jmp     @@DoLine
  3094.  
  3095. @@Invisible:
  3096.     pop     di
  3097.     ret
  3098.  
  3099. @@DoLine:
  3100.     pop     di
  3101.     xchg    cx,ax
  3102.     mov     si,dx
  3103.     mul     ScrnLogicalByteWidth
  3104.     mov     dx,si
  3105.     add     ax,di
  3106.     mov     di,cx
  3107.     sar     di,2
  3108.     add     di,ax
  3109.     and     si,03h
  3110.     mov     ah,byte ptr RightClipPlaneMask[si]
  3111.     mov     si,cx
  3112.     and     si,03h
  3113.     mov     al,byte ptr LeftClipPlaneMask[si]
  3114.     cmp     dx,cx
  3115.     jle     @@Invisible2
  3116.     xchg    cx,dx
  3117.     dec     cx
  3118.     and     dx,not 03h
  3119.     sub     cx,dx
  3120.     js      @@Invisible2
  3121.     shr     cx,2
  3122.     jnz     @@MasksSet
  3123.     and     al,ah
  3124. @@MasksSet:
  3125.     mov     dl,bl
  3126.     mov     bx,ax
  3127.     mov     ah,dl
  3128.     mov     dx,SCINDEX+1
  3129.     mov     al,bl
  3130.     out     dx,al
  3131.     mov     al,ah
  3132.     stosb
  3133.     dec     cx
  3134.     js      @@Invisible2
  3135.     jz      @@RightEnd
  3136.     mov     al,0fh
  3137.     out     dx,al
  3138.     mov     al,ah
  3139.     shr     cx,1
  3140.     rep     stosw
  3141.     adc     cx,cx
  3142.     rep     stosb
  3143.  
  3144. @@RightEnd:
  3145.     mov     al,bh
  3146.     out     dx,al
  3147.     mov     al,ah
  3148.     stosb
  3149. @@Invisible2:
  3150.     ret
  3151.  
  3152. HLineClipR ENDP
  3153.  
  3154. XTriangle PROC FAR X0, Y0, X1, Y1, X2, Y2, Color, PageOffset : Word
  3155. LOCAL DX01, DY01, DX02, DY02, DX12, DY12, DP01, DP02, DP12, XA01, XA02, XA12 : Word
  3156.  
  3157.     push    di
  3158.     push    es
  3159.     push    ds
  3160.     mov     ax,X0
  3161.     mov     bx,Y0
  3162.     mov     cx,X1
  3163.     mov     dx,Y1
  3164.     cmp     bx,dx
  3165.     jl      @@triY0lY1
  3166.     je      @@triY0eY1
  3167.     xchg    ax,cx
  3168.     xchg    bx,dx
  3169.  
  3170. @@triY0lY1:
  3171.     cmp     dx,Y2
  3172.     jg      @@tria
  3173.     jmp     @@trisorted
  3174. @@tria:
  3175.     xchg    cx,X2
  3176.     xchg    dx,Y2
  3177.     cmp     bx,dx
  3178.     jge     @@trib
  3179.     jmp     @@trisorted
  3180. @@trib:
  3181.     je      @@tribot
  3182.     xchg    ax,cx
  3183.     xchg    bx,dx
  3184.     jmp     @@trisorted
  3185. @@triY0eY1:
  3186.     cmp     bx,Y2
  3187.     jl      @@tribot
  3188.     jg      @@tric
  3189.     jmp     @@tridone
  3190. @@tric:
  3191.     xchg    ax,X2
  3192.     xchg    bx,Y2
  3193.     jmp     @@trisorted
  3194.  
  3195. @@tribot:
  3196.     cmp     ax,cx
  3197.     jl      @@tribotsorted
  3198.     jg      @@tribota
  3199.     jmp     @@tridone
  3200. @@tribota:
  3201.     xchg    ax,cx
  3202. @@tribotsorted:
  3203.     cmp     bx,BottomClip
  3204.     jle     @@triboty0ok
  3205.     jmp     @@tridone
  3206. @@triboty0ok:
  3207.     mov     si,Y2
  3208.     cmp     si,TopClip
  3209.     jge     @@triboty2ok
  3210.     jmp     @@tridone
  3211. @@triboty2ok:
  3212.     mov     X0,ax
  3213.     mov     Y0,bx
  3214.     mov     X1,cx
  3215.     mov     Y1,dx
  3216.  
  3217.     mov     bx,Y2
  3218.     sub     bx,Y0
  3219.     mov     DY02,bx
  3220.     mov     ax,X2
  3221.     sub     ax,X0
  3222.     mov     DX02,ax
  3223.     mov     cx,ax
  3224.     cwd
  3225.     idiv    bx
  3226.     cmp     cx,0
  3227.     jge     @@tribot02
  3228.     dec     ax
  3229. @@tribot02:
  3230.     mov     XA02,ax
  3231.     imul    bx
  3232.     sub     cx,ax
  3233.     mov     DP02,cx
  3234.  
  3235.     mov     bx,Y2
  3236.     sub     bx,Y1
  3237.     mov     DY12,bx
  3238.     mov     ax,X2
  3239.     sub     ax,X1
  3240.     mov     DX12,ax
  3241.     mov     cx,ax
  3242.     cwd
  3243.     idiv    bx
  3244.     cmp     cx,0
  3245.     jge     @@tribot12
  3246.     dec     ax
  3247. @@tribot12:
  3248.     mov     XA12,ax
  3249.     imul    bx
  3250.     sub     cx,ax
  3251.     mov     DP12,cx
  3252.  
  3253.     mov     ax,0
  3254.     mov     bx,0
  3255.     mov     cx,Y0
  3256.     mov     si,X0
  3257.     mov     di,X1
  3258.     dec     di
  3259. @@tribotloop:
  3260.     inc     cx
  3261.  
  3262.     add     ax,DP02
  3263.     jle     @@tribotshortl
  3264.     sub     ax,DY02
  3265.     inc     si
  3266. @@tribotshortl:
  3267.     add     si,XA02
  3268.  
  3269.     add     bx,DP12
  3270.     jle     @@tribotshortr
  3271.     sub     bx,DY12
  3272.     inc     di
  3273. @@tribotshortr:
  3274.     add     di,XA12
  3275.  
  3276.     push    di
  3277.     push    si
  3278.     cmp     cx,Y2
  3279.     jl      @@tribotloop
  3280.  
  3281.     jmp     @@tridrawlines
  3282.  
  3283.  
  3284. @@trisorted:
  3285.     cmp     bx,BottomClip
  3286.     jle     @@triy0ok
  3287.     jmp     @@tridone
  3288. @@triy0ok:
  3289.     mov     si,Y2
  3290.     cmp     si,TopClip
  3291.     jge     @@triy2ok
  3292.     jmp     @@tridone
  3293. @@triy2ok:
  3294.     mov     X0,ax
  3295.     mov     Y0,bx
  3296.     mov     X1,cx
  3297.     mov     Y1,dx
  3298.  
  3299.     mov     bx,dx
  3300.     sub     bx,Y0
  3301.     mov     DY01,bx
  3302.     mov     ax,X1
  3303.     sub     ax,X0
  3304.     mov     DX01,ax
  3305.     mov     cx,ax
  3306.     cwd
  3307.     idiv    bx
  3308.     cmp     cx,0
  3309.     jge     @@tripsl01
  3310.     dec     ax
  3311. @@tripsl01:
  3312.     mov     XA01,ax
  3313.     imul    bx
  3314.     sub     cx,ax
  3315.     mov     DP01,cx
  3316.  
  3317.     mov     bx,Y2
  3318.     sub     bx,Y0
  3319.     mov     DY02,bx
  3320.     mov     ax,X2
  3321.     sub     ax,X0
  3322.     mov     DX02,ax
  3323.     mov     cx,ax
  3324.     cwd
  3325.     idiv    bx
  3326.     cmp     cx,0
  3327.     jge     @@tripsl02
  3328.     dec     ax
  3329. @@tripsl02:
  3330.     mov     XA02,ax
  3331.     imul    bx
  3332.     sub     cx,ax
  3333.     mov     DP02,cx
  3334.  
  3335.     mov     bx,Y2
  3336.     sub     bx,Y1
  3337.     jle     @@triconstcomputed
  3338.     mov     DY12,bx
  3339.     mov     ax,X2
  3340.     sub     ax,X1
  3341.     mov     DX12,ax
  3342.     mov     cx,ax
  3343.     cwd
  3344.     idiv    bx
  3345.     cmp     cx,0
  3346.     jge     @@tripsl12
  3347.     dec     ax
  3348. @@tripsl12:
  3349.     mov     XA12,ax
  3350.     imul    bx
  3351.     sub     cx,ax
  3352.     mov     DP12,cx
  3353.  
  3354. @@triconstcomputed:
  3355.     mov     ax,DX01
  3356.     imul    word ptr DY02
  3357.     mov     bx,ax
  3358.     mov     cx,dx
  3359.  
  3360.     mov     ax,DX02
  3361.     imul    word ptr DY01
  3362.     cmp     cx,dx
  3363.     jg      @@tript1rt
  3364.     jl      @@tript1lt
  3365.     cmp     bx,ax
  3366.     ja      @@tript1rt
  3367.     jb      @@tript1lt
  3368.     jmp     @@tridone
  3369.  
  3370. @@tript1lt:
  3371.     mov     ax,0
  3372.     mov     bx,0
  3373.     mov     cx,Y0
  3374.     mov     si,X0
  3375.     mov     di,si
  3376.     dec     si
  3377. @@triltloop:
  3378.     inc     cx
  3379.  
  3380.     add     ax,DP02
  3381.     jle     @@triltshortl
  3382.     sub     ax,DY02
  3383.     inc     si
  3384. @@triltshortl:
  3385.     add     si,XA02
  3386.     add     bx,DP01
  3387.     jle     @@triltshortr
  3388.     sub     bx,DY01
  3389.     inc     di
  3390. @@triltshortr:
  3391.     add     di,XA01
  3392.     push    si
  3393.     push    di
  3394.     cmp     cx,Y1
  3395.     jl      @@triltloop
  3396.     jmp     @@trilbstart
  3397. @@trilbloop:
  3398.     inc     cx
  3399.     add     ax,DP02
  3400.     jle     @@trilbshortl
  3401.     sub     ax,DY02
  3402.     inc     si
  3403. @@trilbshortl:
  3404.     add     si,XA02
  3405.  
  3406.     add     bx,DP12
  3407.     jle     @@trilbshortr
  3408.     sub     bx,DY12
  3409.     inc     di
  3410. @@trilbshortr:
  3411.     add     di,XA12
  3412.  
  3413.     push    si
  3414.     push    di
  3415. @@trilbstart:
  3416.     cmp     cx,Y2
  3417.     jl      @@trilbloop
  3418.     jmp     @@tridrawlines
  3419.  
  3420. @@tript1rt:
  3421.     mov     ax,0
  3422.     mov     bx,0
  3423.     mov     cx,Y0
  3424.     mov     si,X0
  3425.     mov     di,si
  3426.     dec     di
  3427. @@trirtloop:
  3428.     inc     cx
  3429.  
  3430.     add     ax,DP02
  3431.     jle     @@trirtshortl
  3432.     sub     ax,DY02
  3433.     inc     si
  3434. @@trirtshortl:
  3435.     add     si,XA02
  3436.  
  3437.     add     bx,DP01
  3438.     jle     @@trirtshortr
  3439.     sub     bx,DY01
  3440.     inc     di
  3441. @@trirtshortr:
  3442.     add     di,XA01
  3443.  
  3444.     push    di
  3445.     push    si
  3446.     cmp     cx,Y1
  3447.     jl      @@trirtloop
  3448.     jmp     @@trirbstart
  3449.  
  3450. @@trirbloop:
  3451.     inc     cx
  3452.     add     ax,DP02
  3453.     jle     @@trirbshortl
  3454.     sub     ax,DY02
  3455.     inc     si
  3456. @@trirbshortl:
  3457.     add     si,XA02
  3458.  
  3459.     add     bx,DP12
  3460.     jle     @@trirbshorts
  3461.     sub     bx,DY12
  3462.     inc     di
  3463. @@trirbshorts:
  3464.     add     di,XA12
  3465.  
  3466.     push    di
  3467.     push    si
  3468. @@trirbstart:
  3469.     cmp     cx,Y2
  3470.     jl      @@trirbloop
  3471.  
  3472. @@tridrawlines:
  3473.     mov     cx,SCREENSEG
  3474.     mov     es,cx
  3475.     mov     dx,SCINDEX
  3476.     mov     al,MAPMASK
  3477.     out     dx,al
  3478.  
  3479. @@lineloop:
  3480.     pop     ax
  3481.     pop     dx
  3482.     cmp     ax,dx
  3483.     jg      @@tridrawnext
  3484.     mov     bx,Color
  3485.     mov     cx,Y2
  3486.     add     dx,2
  3487.     mov     di,PageOffset
  3488.     call    HLineClipR
  3489. @@tridrawnext:
  3490.     dec     word ptr Y2
  3491.     dec     word ptr DY02
  3492.     jnz     @@lineloop
  3493.  
  3494. @@tridone:
  3495.     pop     ds
  3496.     pop     es
  3497.     pop     di
  3498.     ret
  3499.  
  3500. XTriangle ENDP
  3501.  
  3502. XPolygon PROC FAR vertices : DWord, numvertices : Word, Color : Word,\
  3503.                                     PageOffset : Word
  3504. LOCAL x0 : Word, y0 : Word, tricount : Word
  3505.  
  3506.     mov   cx,numvertices
  3507.     cmp   cx,3
  3508.     jl    @@Done
  3509.     sub   cx,3
  3510.     mov   tricount,cx
  3511.     les   di,vertices
  3512.     mov   ax,es:[di]
  3513.     mov   x0,ax
  3514.     mov   ax,es:[di+2]
  3515.     mov   y0,ax
  3516.  
  3517. @@NextTriangle:
  3518.     add   di, 4
  3519.     mov   ax, x0
  3520.     push  ax
  3521.     mov   ax, y0
  3522.     push  ax
  3523.     mov   ax, es:[di+4]
  3524.     push  ax
  3525.     mov   ax, es:[di+6]
  3526.     push  ax
  3527.     mov   ax,es:[di]
  3528.     push  ax
  3529.     mov   ax,es:[di+2]
  3530.     push  ax
  3531.     mov   ax, color
  3532.     push  ax
  3533.     mov   ax, pageoffset
  3534.     push  ax
  3535.     call  xtriangle
  3536.     dec   tricount
  3537.     jns   @@NextTriangle
  3538. @@Done:
  3539.     ret
  3540.  
  3541. XPolygon ENDP
  3542.  
  3543. XPutCursor PROC FAR X : Word, Y : Word, ATopClip : Word, ABottomClip : Word,\
  3544.                                         ScrnOffs : Word
  3545. LOCAL Height : Word, TopRow : Word, NextLineIncr : Word
  3546.  
  3547.     push  si
  3548.     push  di
  3549.     push  ds
  3550.     mov   ax,seg @data
  3551.     mov   ds,ax
  3552.     cld
  3553.     mov   ax,14
  3554.     mov   bx,Y
  3555.     mov   dx,ATopClip
  3556.     sub   dx,bx
  3557.     jle   @@NotTopClip
  3558.     cmp   dx,ax
  3559.     jnl   @@NotVisible
  3560.     mov   cx,dx
  3561.     sub   ax,dx
  3562.     add   bx,dx
  3563.     jmp   @@VertClipDone
  3564.  
  3565. @@NotVisible:
  3566.     pop   ds
  3567.     pop   di
  3568.     pop   si
  3569.     jmp   @@Done
  3570.  
  3571. @@NotTopClip:
  3572.     mov   dx,ABottomClip
  3573.     sub   dx,bx
  3574.     js    @@NotVisible
  3575.     mov   cx,0
  3576.     cmp   dx,ax
  3577.     jg    @@VertClipDone
  3578.     inc   dx
  3579.     mov   ax,dx
  3580.  
  3581. @@VertClipDone:
  3582.     mov   Height,ax
  3583.     mov   TopRow,cx
  3584.     mov   ax,SCREENSEG
  3585.     mov   es,ax
  3586.     mov   ax,bx
  3587.     mov   cx,ScrnLogicalByteWidth
  3588.     mul   cx
  3589.     mov   di,ax
  3590.     sub   cx,3
  3591.     mov   NextLineIncr,cx
  3592.     mov   cx,X
  3593.     mov   bx,cx
  3594.     shr   cx,2
  3595.     add   di,cx
  3596.     and   bx,3
  3597.     add   di,ScrnOffs
  3598.     mov   ax,42
  3599.     mul   bx
  3600.     mov   si,offset MouseMask
  3601.     add   si,ax
  3602.     mov   ax,3
  3603.     mul   TopRow
  3604.     add   si,ax
  3605.     mov   dx,SCINDEX
  3606.     mov   al,MAPMASK
  3607.     out   dx,al
  3608.     inc   dx
  3609.     mov   ah,byte ptr Height
  3610.     mov   bl,MouseColor
  3611.  
  3612. @@RowLoop:
  3613.     mov   cx,3
  3614.  
  3615. @@ColLoop:
  3616.     lodsb
  3617.     out   dx,al
  3618.     mov   es:[di],bl
  3619.     inc   di
  3620.     loop  @@ColLoop
  3621.  
  3622.     add   di,NextLineIncr
  3623.     dec   ah
  3624.     jnz   @@RowLoop
  3625.  
  3626.     pop   ds
  3627.     pop   di
  3628.     pop   si
  3629.  
  3630. @@Done:
  3631.     ret
  3632.  
  3633. ENDP
  3634.  
  3635. GetBG PROC NEAR
  3636.  
  3637.     push  ds
  3638.     cld
  3639.     mov   cx,ScrnLogicalByteWidth
  3640.     mul   cx
  3641.     add   si,ax
  3642.     sub   cx,3
  3643.     shr   bx,2
  3644.     add   si,bx
  3645.     mov   bx,cx
  3646.     mov   di,BGSaveOffs
  3647.     mov   ax,SCREENSEG
  3648.     mov   es,ax
  3649.     mov   ds,ax
  3650.     mov   dx,GCINDEX
  3651.     mov   ax,BITMASK
  3652.     out   dx,ax
  3653.     mov   dx,SCINDEX
  3654.     mov   al,MAPMASK
  3655.     out   dx,al
  3656.     inc   dx
  3657.     mov   al,0fh
  3658.     out   dx,al
  3659.     mov   cx,14
  3660. @@Loop:
  3661.     movsb
  3662.     movsb
  3663.     movsb
  3664.     add si,bx
  3665.     loop @@Loop
  3666.     mov  dx,GCINDEX+1
  3667.     mov  al,0ffh
  3668.     out  dx,al
  3669.     pop  ds
  3670.     ret
  3671.  
  3672. GetBg ENDP
  3673.  
  3674. RestoreBG PROC NEAR
  3675.  
  3676.     push  ds
  3677.     cld
  3678.     mov   cx,ScrnLogicalByteWidth
  3679.     mul   cx
  3680.     add   di,ax
  3681.     sub   cx,3
  3682.     shr   bx,2
  3683.     add   di,bx
  3684.     mov   si,BGSaveOffs
  3685.     mov   ax,SCREENSEG
  3686.     mov   es,ax
  3687.     mov   ds,ax
  3688.     mov   dx,GCINDEX
  3689.     mov   ax,BITMASK
  3690.     out   dx,ax
  3691.     mov   dx,SCINDEX
  3692.     mov   al,MAPMASK
  3693.     out   dx,al
  3694.     inc   dx
  3695.     mov   al,0fh
  3696.     out   dx,al
  3697.     mov   bx,cx
  3698.     mov   cx,14
  3699. @@Loop:
  3700.     movsb
  3701.     movsb
  3702.     movsb
  3703.     add di,bx
  3704.     loop @@Loop
  3705.     mov  dx,GCINDEX+1
  3706.     mov  al,0ffh
  3707.     out  dx,al
  3708.     pop   ds
  3709.     ret
  3710.  
  3711. RestoreBg ENDP
  3712.  
  3713. UpdateCursor PROC FAR
  3714.     cmp VSyncHandlerActive,1
  3715.     je  @@SkipVS
  3716.     mov dx,InputStatus0
  3717. @@WaitNotVsync:
  3718.     in  al,dx
  3719.     test al,08h
  3720.     jnz @@WaitNotVSync
  3721. @@WaitVSync:
  3722.     in  al,dx
  3723.     test al,08h
  3724.     jz  @@WaitVSync
  3725. @@SkipVS:
  3726.     mov di,OldScrnOffs
  3727.     mov ax,OldY
  3728.     mov bx,OldX
  3729.     call restorebg
  3730.     mov si,VisiblePageOffs
  3731.     mov ax,MouseY
  3732.     mov bx,MouseX
  3733.     mov OldScrnOffs,si
  3734.     mov OldY,ax
  3735.     mov OldX,bx
  3736.     call getbg
  3737.     push OldX
  3738.     push OldY
  3739.     mov  ax,0
  3740.     push ax
  3741.     mov  ax,ScrnPhysicalHeight
  3742.     push ax
  3743.     push VisiblePageOffs
  3744.     call xputcursor
  3745.     ret
  3746.  
  3747. UpdateCursor ENDP
  3748.  
  3749. MouseHandler PROC FAR
  3750.  
  3751.     push  ds
  3752.     mov   di,seg @data
  3753.     mov   ds,di
  3754.     cmp   inhandler,1
  3755.     jne   @@NotActive
  3756.     jmp   @@Done2
  3757. @@NotActive:
  3758.     mov   inhandler,1
  3759.     mov   MouseButtonStatus,bx
  3760.     test  ax,1
  3761.     jz    @@Done
  3762.     shr   cx,1
  3763.     mov   MouseX,cx
  3764.     mov   MouseY,dx
  3765.     cmp   MouseHidden,1
  3766.     je    @@Done
  3767.     cmp   MouseFrozen,1
  3768.     je    @@Done
  3769.     cmp   VSyncHandlerActive,1
  3770.     je    @@VSyncHandle
  3771.     call  updatecursor
  3772.     jmp   short @@Done
  3773. @@VSyncHandle:
  3774.     mov   MouseRefreshFlag,1
  3775. @@Done:
  3776.     mov   inhandler,0
  3777. @@Done2:
  3778.     pop  ds
  3779.     ret
  3780.  
  3781. MouseHandler ENDP
  3782.  
  3783. XDefineMouseCursor PROC FAR MouseDef : DWord, MouseColour : Byte
  3784.  
  3785.     cmp   MouseInstalled,0
  3786.     je    @@Done
  3787.     mov   al,MouseColour
  3788.     mov   MouseColor,al
  3789.     push  si
  3790.     push  di
  3791.     push  ds
  3792.     mov   ax,ds
  3793.     mov   es,ax
  3794.     mov   di,offset MouseMask
  3795.     lds   si,MouseDef
  3796.     xor   cl,cl
  3797. @@AlignmentLoop:
  3798.     push  si
  3799.     mov   dh,14
  3800. @@RowLoop:
  3801.     lodsb
  3802.     xor   ah,ah
  3803.     shl   ax,cl
  3804.     mov   bl,al
  3805.     and   bl,0fh
  3806.     mov   es:[di],bl
  3807.     inc   di
  3808.     shr   al,4
  3809.     stosw
  3810.     dec   dh
  3811.     jnz   @@RowLoop
  3812.     pop   si
  3813.     inc   cl
  3814.     cmp   cl,4
  3815.     jne   @@AlignmentLoop
  3816.     pop   ds
  3817.     pop   di
  3818.     pop   si
  3819. @@Done:
  3820.     ret
  3821.  
  3822. XDefineMouseCursor ENDP
  3823.  
  3824.  
  3825. XMouseInit PROC FAR
  3826.  
  3827.     mov   InHandler,0
  3828.     mov   mousehidden,0
  3829.     mov   BGSaveOffs,0
  3830.     mov   MouseX,0
  3831.     mov   MOuseY,0
  3832.     mov   MouseButtonStatus,0
  3833.     mov   MouseFrozen,0
  3834.     mov   MouseColor,0
  3835.     mov   MouseInstalled,0
  3836.     cmp   MouseButtonCount,0
  3837.     jne   @@DontInitialize
  3838.     xor   ax,ax
  3839.     int   33h
  3840.     or    ax,ax
  3841.     jz    @@Done
  3842.     mov   MouseButtonCount,bx
  3843.  
  3844. @@DontInitialize:
  3845.     mov   MouseInstalled,ax
  3846.     or    ax,ax
  3847.     jz    @@Done
  3848.     mov   ax,NonVisualOffs
  3849.     mov   BGSaveOffs,ax
  3850.     add   ax,42
  3851.     mov   NonVisualOffs,ax
  3852.     mov   ax,02
  3853.     int   33h
  3854.     mov   MouseInstalled,1
  3855.     mov   ax,07h
  3856.     mov   cx,0
  3857.     mov   dx,ScrnPhysicalPixelWidth
  3858.     shl   dx,1
  3859.     int   33h
  3860.     mov   ax,08h
  3861.     mov   cx,0
  3862.     mov   dx,ScrnPhysicalHeight
  3863.     int   33h
  3864.     mov   ax,0fh
  3865.     mov   cx,4
  3866.     mov   dx,8
  3867.     int   33h
  3868.     mov   ax,3
  3869.     int   33h
  3870.     mov   MouseY,dx
  3871.     shr   cx,1
  3872.     mov   MouseX,cx
  3873.     mov   ax,12
  3874.     mov   bx,seg mousehandler
  3875.     mov   es,bx
  3876.     mov   dx,offset mousehandler
  3877.     mov   cx,1fh
  3878.     int   33h
  3879.     mov   MouseHidden,1
  3880.     push  ds
  3881.     mov   ax,offset InitMouseDef
  3882.     push  ax
  3883.     mov   al, MouseColor
  3884.     push  ax
  3885.     call  xdefinemousecursor
  3886.     cmp   VSyncHandlerActive,1
  3887.     jne   @@AllIsWell
  3888.     mov   ax, offset updatecursor
  3889.     mov   dx, seg updatecursor
  3890.     cli
  3891.     mov   word ptr [MouseVSyncHandler],ax
  3892.     mov   word ptr [MouseVsyncHandler+2],dx
  3893.     mov   MouseRefreshFlag,0
  3894.     sti
  3895. @@AllIsWell:
  3896.     mov   ax,MouseInstalled
  3897. @@Done:
  3898.     ret
  3899.  
  3900. XMouseInit ENDP
  3901.  
  3902. XMouseWindow PROC FAR x0 : Word, y0 : Word, x1 : Word, y1 : Word
  3903.  
  3904.     mov   ax,7
  3905.     mov   cx,x0
  3906.     shl   cx,1
  3907.     mov   dx,x1
  3908.     shl   dx,1
  3909.     int   33h
  3910.     mov   ax,8
  3911.     mov   cx,y0
  3912.     mov   dx,y1
  3913.     int   33h
  3914.     ret
  3915.  
  3916. XMouseWindow ENDP
  3917.  
  3918.  
  3919. XShowMouse PROC FAR
  3920.  
  3921.     cmp   MouseInstalled,0
  3922.     je    @@Done
  3923.     cmp   MouseHidden,0
  3924.     je    @@Done
  3925.     push  si
  3926.     push  di
  3927.  
  3928. @@WaitEndOfHandler:
  3929.     mov   cl,inhandler
  3930.     or    cl,cl
  3931.     jnz   @@WaitEndOfHandler
  3932.     mov   si,VisiblePageOffs
  3933.     mov   ax,MouseY
  3934.     mov   bx,MouseX
  3935.     mov   OldScrnOffs,si
  3936.     mov   OldY,ax
  3937.     mov   OldX,bx
  3938.     call  getbg
  3939.     push  OldX
  3940.     push  OldY
  3941.     xor   ax,ax
  3942.     push  ax
  3943.     push  ScrnLogicalHeight
  3944.     push  VisiblePageOffs
  3945.     call  xputcursor
  3946.     mov   MouseHidden,0
  3947.     pop   di
  3948.     pop   si
  3949. @@Done:
  3950.     ret
  3951.  
  3952. XShowMouse ENDP
  3953.  
  3954. XHideMouse PROC FAR
  3955.  
  3956.     cmp   MouseInstalled,0
  3957.     je    @@Done
  3958.     cmp   MouseHidden,0
  3959.     jne   @@Done
  3960.     push  si
  3961.     push  di
  3962. @@WaitEndOfHandler:
  3963.     mov   cl,inhandler
  3964.     or    cl,cl
  3965.     jnz   @@WaitEndOfHandler
  3966.     mov   MouseHidden,TRUE
  3967.     mov   di,OldScrnOffs
  3968.     mov   ax,OldY
  3969.     mov   bx,OldX
  3970.     call  restorebg
  3971.     pop   di
  3972.     pop   si
  3973. @@Done:
  3974.     ret
  3975.  
  3976. XHideMouse ENDP
  3977.  
  3978. XMouseRemove PROC FAR
  3979.  
  3980.     cmp   MouseInstalled,0
  3981.     je    @@Done
  3982.     call  xhidemouse
  3983.     mov   ax,12
  3984.     xor   cx,cx
  3985.     int   33h
  3986.     mov   MouseRefreshFlag,0
  3987.     mov   MouseInstalled,0
  3988. @@Done:
  3989.     ret
  3990.  
  3991. XMouseRemove ENDP
  3992.  
  3993. XPositionMouse PROC FAR X : Word, Y : Word
  3994.  
  3995. @@WaitEndOfHandler:
  3996.     mov   bl,inhandler
  3997.     or    bl,bl
  3998.     jnz   @@WaitEndOfHandler
  3999.     mov   ax,4
  4000.     mov   cx,X
  4001.     mov   dx,Y
  4002.     mov   MouseX,cx
  4003.     mov   MouseY,dx
  4004.     shl   cx,1
  4005.     mov   inhandler,1
  4006.     int   33h
  4007.     cmp   MouseHidden,FALSE
  4008.     jne   @@NotVisible
  4009.     push  di
  4010.     push  si
  4011.     call  updatecursor
  4012.     pop   si
  4013.     pop   di
  4014. @@NotVisible:
  4015.     mov   inhandler,0
  4016.     ret
  4017.  
  4018. XPositionMouse ENDP
  4019.  
  4020. XUpdateMouse PROC FAR
  4021.  
  4022.     cmp   MouseInstalled,0
  4023.     je    @@Done
  4024.     cmp   MouseHidden,0
  4025.     jne   @@Done
  4026.     mov   ax,03h
  4027.     int   33h
  4028.     shr   cx,1
  4029.     mov   MouseX,cx
  4030.     mov   MouseY,dx
  4031.     mov   MouseButtonStatus,bx
  4032.     cmp   VSyncHandlerActive,1
  4033.     je    @@HandleVSync
  4034.     call  updatecursor
  4035.     jmp   short @@Done
  4036. @@HandleVSync:
  4037.     mov   MouseRefreshFlag,1
  4038. @@Done:
  4039.     ret
  4040.  
  4041. XUpdateMouse ENDP
  4042.  
  4043.  
  4044. XFloodFill PROC FAR X : Word, Y : Word, PgOfs : Word, Color : Word
  4045. LOCAL len : Word, y1 : Word, y2 : Word, deltax : Word, floodval : Word,\
  4046.             Astackptr : Word, FillCount : Word
  4047.  
  4048.     mov  FillCount,0
  4049.     mov  si,Y
  4050.     mov  ax,ScrnLogicalByteWidth
  4051.     mul  si
  4052.     mov  di,X
  4053.     mov  bx,di
  4054.     shr  di,2
  4055.     add  di,ax
  4056.     add  di,PgOfs
  4057.     mov  ax,SCREENSEG
  4058.     mov  es,ax
  4059.     mov  ah,bl
  4060.     and  ah,011b
  4061.     mov  al,READMAP
  4062.     mov  dx,GCINDEX
  4063.     out  dx,ax
  4064.  
  4065.     mov  al,es:[di]
  4066.     cmp  al,byte ptr Color
  4067.     je   @@Done
  4068.  
  4069.     mov  cx,LeftClip
  4070.     sal  cx,2
  4071.     cmp  bx,cx
  4072.     jl   @@Done
  4073.  
  4074.     mov  cx,RightClip
  4075.     sal  cx,2
  4076.     cmp  bx,cx
  4077.     jg   @@Done
  4078.  
  4079.     mov  floodval,ax
  4080.  
  4081.     push bx
  4082.     push si
  4083.     push si
  4084.     mov  cx,1
  4085.     push cx
  4086.     mov  Astackptr,1
  4087.  
  4088.     mov  deltax,-1
  4089.     mov  y1,si
  4090.     mov  y2,si
  4091.     jmp  @@entry
  4092.  
  4093. @@Done2:
  4094.     jmp @@Done
  4095.  
  4096. @@NextScanCol:
  4097.     dec  Astackptr
  4098.     js   @@Done2
  4099.  
  4100. @@WhileLoop:
  4101.     pop  cx
  4102.     mov  deltax,cx
  4103.     pop  ax
  4104.     mov  y2,ax
  4105.     pop  si
  4106.     mov  y1,si
  4107.     pop  bx
  4108.  
  4109.     sub  ax,si
  4110.     jns  @@PositiveY
  4111.     neg  ax
  4112. @@PositiveY:
  4113.     add  FillCount,ax
  4114.     add  bx,cx
  4115.     mov  ax,bx
  4116.     sar  ax,2
  4117.     cmp  ax,LeftClip
  4118.     jl   @@NextScanCol
  4119.     cmp  ax,RightClip
  4120.     jg   @@NextScanCol
  4121.  
  4122.     mov  ah,bl
  4123.     and  ah,011b
  4124.     mov  al,READMAP
  4125.     mov  dx,GCINDEX
  4126.     out  dx,ax
  4127.  
  4128. @@entry:
  4129.     mov  cl,bl
  4130.     and  cl,011b
  4131.     mov  ax,0100h + MAPMASK
  4132.     shl  ah,cl
  4133.     mov  dx,SCINDEX
  4134.     out  dx,ax
  4135.     mov  ax,ScrnLogicalByteWidth
  4136.     mov  cx,ax
  4137.     mul  si
  4138.     mov  di,bx
  4139.     shr  di,2
  4140.     add  di,ax
  4141.     add  di,PgOfs
  4142.     mov  dx,di
  4143.  
  4144.     mov  al,byte ptr Color
  4145.     mov  ah,byte ptr floodval
  4146.  
  4147. @@FillColUpward:
  4148.     cmp  si,TopClip
  4149.     jl   @@UpwardFillDone
  4150.  
  4151.     cmp  es:[di],ah
  4152.     jne  @@UpwardFillDone
  4153.  
  4154.     mov  es:[di],al
  4155.     sub  di,cx
  4156.     dec  si
  4157.     jmp  @@FillColUpward
  4158.  
  4159. @@UpwardFillDone:
  4160.     cmp  si,y1
  4161.     jge  @@Skip
  4162.  
  4163.     inc  si
  4164.     mov  len,si
  4165.  
  4166.     cmp  si,y1
  4167.     jge  @@AtColumnTop
  4168.  
  4169.     push bx
  4170.     push si
  4171.     mov  ax,y1
  4172.     dec  ax
  4173.     push ax
  4174.     mov  ax,deltax
  4175.     neg  ax
  4176.     push ax
  4177.     inc  Astackptr
  4178.  
  4179. @@AtColumnTop:
  4180.     mov  si,y1
  4181.     mov  di,dx
  4182.     add  di,cx
  4183.     inc  si
  4184.  
  4185.  
  4186. @@ColumnLoop:
  4187.     mov  ah,byte ptr floodval
  4188.     mov  al,byte ptr Color
  4189.  
  4190. @@DownwardFill:
  4191.     cmp  si,BottomClip
  4192.     jg   @@DownwardFillDone
  4193.     cmp  es:[di],ah
  4194.     jne  @@DownwardFillDone
  4195.     mov  es:[di],al
  4196.     add  di,cx
  4197.     inc  si
  4198.     jmp  @@DownwardFill
  4199.  
  4200. @@DownwardFillDone:
  4201.  
  4202.     push bx
  4203.     mov  ax,len
  4204.     push ax
  4205.     mov  ax,si
  4206.     dec  ax
  4207.     push ax
  4208.     mov  ax,deltax
  4209.     push ax
  4210.     inc  Astackptr
  4211.  
  4212.     mov  ax,y2
  4213.     inc  ax
  4214.     cmp  si,ax
  4215.     jle  @@Skip
  4216.  
  4217.     push bx
  4218.     push ax
  4219.     mov  ax,si
  4220.     dec  ax
  4221.     push ax
  4222.     mov  ax,deltax
  4223.     neg  ax
  4224.     push ax
  4225.     inc  Astackptr
  4226.  
  4227. @@Skip:
  4228.     mov  ah,byte ptr floodval
  4229.     mov  dx,y2
  4230.  
  4231. @@Backtrack:
  4232.     add  di,cx
  4233.     inc  si
  4234.     cmp  si,dx
  4235.     jg   @@BacktrackDone
  4236.  
  4237.     cmp  byte ptr es:[di],ah
  4238.     jne  @@Backtrack
  4239.  
  4240. @@BacktrackDone:
  4241.     mov  len,si
  4242.     cmp  si,dx
  4243.     jle  @@ColumnLoop
  4244.  
  4245.     dec  Astackptr
  4246.     js   @@Done
  4247.     jmp  @@WhileLoop
  4248. @@Done:
  4249.     mov  ax,FillCount
  4250.     ret
  4251.  
  4252. XFloodFill ENDP
  4253.  
  4254.  
  4255. XBoundaryFill PROC FAR X : Word, Y : Word, PgOfs : Word, BoundaryColor : Word,\
  4256.                                              Color : Word
  4257. LOCAL len : Word, y1 : Word, y2 : Word, deltax : Word, y1offs : Word,\
  4258.             Astackptr : Word, FillCount : Word
  4259.  
  4260.     mov  FillCount,0
  4261.     mov  si,Y
  4262.     mov  ax,ScrnLogicalByteWidth
  4263.     mul  si
  4264.     mov  di,X
  4265.     mov  bx,di
  4266.     shr  di,2
  4267.     add  di,ax
  4268.     add  di,PgOfs
  4269.     mov  ax,SCREENSEG
  4270.     mov  es,ax
  4271.     mov  ah,bl
  4272.     and  ah,011b
  4273.     mov  al,READMAP
  4274.     mov  dx,GCINDEX
  4275.     out  dx,ax
  4276.     mov  al,es:[di]
  4277.     cmp  al,byte ptr Color
  4278.     je   @@Done
  4279.     cmp  al,byte ptr BoundaryColor
  4280.     je   @@Done
  4281.  
  4282.  
  4283.     mov  cx,LeftClip
  4284.     sal  cx,2
  4285.     cmp  bx,cx
  4286.     jl   @@Done
  4287.  
  4288.     mov  cx,RightClip
  4289.     sal  cx,2
  4290.     cmp  bx,cx
  4291.     jg   @@Done
  4292.  
  4293.     push bx
  4294.     push si
  4295.     push si
  4296.     mov  cx,1
  4297.     push cx
  4298.     mov  Astackptr,1
  4299.     mov  al,byte ptr BoundaryColor
  4300.     mov  byte ptr [Color+1],al
  4301.  
  4302.     mov  deltax,-1
  4303.     mov  y1,si
  4304.     mov  y2,si
  4305.     jmp  @@entry
  4306.  
  4307. @@NextScanCol:
  4308.     dec  Astackptr
  4309.     js   @@Done
  4310.  
  4311. @@WhileLoop:
  4312.     pop  cx
  4313.     mov  deltax,cx
  4314.     pop  ax
  4315.     mov  y2,ax
  4316.     pop  si
  4317.     mov  y1,si
  4318.     pop  bx
  4319.     add  bx,cx
  4320.  
  4321.     sub  ax,si
  4322.     jns  @@PositiveY
  4323.     neg  ax
  4324. @@PositiveY:
  4325.     add  FillCount,ax
  4326.     mov  ax,bx
  4327.     sar  ax,2
  4328.     cmp  ax,LeftClip
  4329.     jl   @@NextScanCol
  4330.     cmp  ax,RightClip
  4331.     jg   @@NextScanCol
  4332.     mov  ah,bl
  4333.     and  ah,011b
  4334.     mov  al,READMAP
  4335.     mov  dx,GCINDEX
  4336.     out  dx,ax
  4337.  
  4338. @@entry:
  4339.     mov  cl,bl
  4340.     and  cl,011b
  4341.     mov  ax,0100h + MAPMASK
  4342.     shl  ah,cl
  4343.     mov  dx,SCINDEX
  4344.     out  dx,ax
  4345.     mov  ax,ScrnLogicalByteWidth
  4346.     mov  cx,ax
  4347.     mul  si
  4348.     mov  di,bx
  4349.     shr  di,2
  4350.     add  di,ax
  4351.     add  di,PgOfs
  4352.     mov  y1offs,di
  4353.     mov   ax,Color
  4354.  
  4355. @@FillColUpward:
  4356.     cmp  si,TopClip
  4357.     jl   @@UpwardFillDone
  4358.  
  4359.     mov  dl,es:[di]
  4360.     cmp  dl,ah
  4361.     je   @@UpwardFillDone
  4362.  
  4363.     cmp  dl,al
  4364.     je   @@UpwardFillDone
  4365.  
  4366.     mov  es:[di],al
  4367.     sub  di,cx
  4368.     dec  si
  4369.     jmp  @@FillColUpward
  4370.  
  4371. @@UpwardFillDone:
  4372.     cmp  si,y1
  4373.     jge  @@Skip
  4374.  
  4375.     inc  si
  4376.     mov  len,si
  4377.  
  4378.     cmp  si,y1
  4379.     jge  @@AtColumnTop
  4380.  
  4381.     push bx
  4382.     push si
  4383.     mov  ax,y1
  4384.     dec  ax
  4385.     push ax
  4386.     mov  ax,deltax
  4387.     neg  ax
  4388.     push ax
  4389.     inc  Astackptr
  4390.  
  4391. @@AtColumnTop:
  4392.     mov  si,y1
  4393.     mov  di,y1offs
  4394.     add  di,cx
  4395.     inc  si
  4396.  
  4397.  
  4398. @@ColumnLoop:
  4399.     mov   ax,Color
  4400.  
  4401. @@DownwardFill:
  4402.     cmp  si,BottomClip
  4403.     jg   @@DownwardFillDone
  4404.  
  4405.     cmp  es:[di],ah
  4406.     je   @@DownwardFillDone
  4407.  
  4408.     cmp  es:[di],al
  4409.     je   @@DownwardFillDone
  4410.  
  4411.     mov  es:[di],al
  4412.     add  di,cx
  4413.     inc  si
  4414.     jmp  @@DownwardFill
  4415.  
  4416. @@DownwardFillDone:
  4417.  
  4418.     push bx
  4419.     mov  ax,len
  4420.     push ax
  4421.     mov  ax,si
  4422.     dec  ax
  4423.     push ax
  4424.     mov  ax,deltax
  4425.     push ax
  4426.     inc  Astackptr
  4427.  
  4428.     mov  ax,y2
  4429.     inc  ax
  4430.     cmp  si,ax
  4431.     jle  @@Skip
  4432.  
  4433.     push bx
  4434.     push ax
  4435.     mov  ax,si
  4436.     dec  ax
  4437.     push ax
  4438.     mov  ax,deltax
  4439.     neg  ax
  4440.     push ax
  4441.     inc  Astackptr
  4442.  
  4443. @@Skip:
  4444.     mov  ax,Color
  4445.  
  4446. @@Backtrack:
  4447.     add  di,cx
  4448.     inc  si
  4449.     cmp  si,y2
  4450.     jg   @@BacktrackDone
  4451.  
  4452.     mov  dl,byte ptr es:[di]
  4453.     cmp  dl,al
  4454.     je   @@Backtrack
  4455.  
  4456.     cmp  dl,ah
  4457.     je   @@Backtrack
  4458.  
  4459. @@BacktrackDone:
  4460.     mov  len,si
  4461.     cmp  si,y2
  4462.     jle  @@ColumnLoop
  4463.  
  4464.     dec  Astackptr
  4465.     js   @@Done
  4466.     jmp  @@WhileLoop
  4467. @@Done:
  4468.     mov  ax,FillCount
  4469.     ret
  4470.  
  4471. XBoundaryFill ENDP
  4472.  
  4473. getvsyncperiod proc near
  4474.     mov    al,TIMERMODE            ;Start timer
  4475.     out    TIMERCONTROL,al
  4476.     mov    al,0
  4477.     out    TIMER0,al
  4478.     out    TIMER0,al
  4479.  
  4480.     WaitVsyncStart
  4481.  
  4482.     mov    al,LATCHCOUNT
  4483.     out    TIMERCONTROL,al
  4484.     in     al,TIMER0
  4485.     mov    cl,al
  4486.     in     al,TIMER0
  4487.     mov    ch,al                    ;cx=65536-clicks
  4488.  
  4489.     WaitVsyncStart
  4490.  
  4491.     mov    al,LATCHCOUNT
  4492.     out    TIMERCONTROL,al
  4493.     in     al,TIMER0
  4494.     mov    dl,al
  4495.     in     al,TIMER0
  4496.     mov    dh,al
  4497.  
  4498.     sub    cx,dx
  4499.     mov    ax,cx
  4500.     ret
  4501. getvsyncperiod endp
  4502.  
  4503. vsyncint proc far
  4504.     pusha
  4505.     push   ds
  4506.     push   es
  4507.  
  4508.     mov    ax,@data
  4509.     mov    ds,ax
  4510.     add    VsyncIntTicksLo,1
  4511.     adc    VsyncIntTicksHi,0
  4512.  
  4513.     inc    ElapsedVrts
  4514.     mov    cx,ElapsedVrts
  4515.     cmp    cx,VrtsToSkip
  4516.     jl     @@StopClock
  4517.  
  4518.     cmp    StartAddressFlag,1
  4519.     jne    @@StopClock
  4520.  
  4521.     mov    dx,CRTCINDEX
  4522.     mov    ax,WaitingStartLow
  4523.     mov    bx,WaitingStartHigh
  4524.     out    dx,ax
  4525.     mov    ax,bx
  4526.     out    dx,ax
  4527.  
  4528. @@StopClock:
  4529.     cli
  4530.     mov    al,TIMERMODE
  4531.     out    TIMERCONTROL,al
  4532.     mov    al,255
  4533.     out    TIMER0,al
  4534.     out    TIMER0,al
  4535.     sti
  4536.  
  4537.     cli
  4538.     mov    dx,INPUTSTATUS0
  4539. @@WaitVS:
  4540.     in     al,dx
  4541.     test   al,08h
  4542.     jz     @@WaitVS
  4543.  
  4544.     mov    al,TIMERMODE
  4545.     out    TIMERCONTROL,al
  4546.     mov    ax,ClockRate
  4547.     out    TIMER0,al
  4548.     mov    al,ah
  4549.     out    TIMER0,al
  4550.  
  4551.     cmp    cx,VrtsToSkip
  4552.     jl     @@PaletteInt
  4553.  
  4554.     cmp    StartAddressFlag,1
  4555.     jne    @@PaletteInt
  4556.  
  4557.     xor    cx,cx
  4558.     mov    ElapsedVrts,cx
  4559.  
  4560.     mov    ax,WaitingPelPan
  4561.     mov    dx,ACINDEX
  4562.     out    dx,al
  4563.     mov    al,ah
  4564.     out    dx,al
  4565.     ;mov    StartAddressFlag,0
  4566.  
  4567. @@PaletteInt:
  4568.     mov    StartAddressFlag,0
  4569.     cmp    VsyncPaletteCount,0
  4570.     je     @@MouseInt
  4571.     mov    si, offset VsyncPaletteBuffer
  4572.     mov    cx, VsyncPaletteCount
  4573.     mov    ax, cx
  4574.     shl    cx, 1
  4575.     add    cx, ax
  4576.     mov    ax, VsyncPaletteStart
  4577.     mov    dx, DACWRITEINDEX
  4578.     out    dx, al
  4579.     mov    dx, DACDATA
  4580.  
  4581.     rep    outsb
  4582.     mov    VsyncPaletteCount,0
  4583.  
  4584. @@MouseInt:
  4585.     cmp    MouseRefreshFlag,1
  4586.     jne    @@UserInt
  4587.     call   dword ptr [MouseVsyncHandler]
  4588.     mov    MouseRefreshFlag,0
  4589.  
  4590. @@UserInt:
  4591.     cmp    UserVsyncSeg, 0
  4592.     je     short @@Sim182
  4593.     cmp    InUserHandler,0
  4594.     jne    short @@Sim182
  4595.     mov    InUserHandler,1
  4596.     mov    StackSeg,ss
  4597.     mov    StackPtr,sp
  4598.     push   ds
  4599.     pop    ss
  4600.     mov    sp, offset LocalStack
  4601.     sti
  4602.     call   dword ptr [UserVsyncHandler]
  4603.     cli
  4604.     mov    sp, StackPtr
  4605.     mov    ss, StackSeg
  4606.     mov    InUserHandler,0
  4607.  
  4608. @@Sim182:
  4609.     mov    ax,[VsyncPeriod]
  4610.     add    [ClockCounter],ax
  4611.     jnc    short @@DontChainOld
  4612.     pop    es
  4613.     pop    ds
  4614.     popa
  4615.     sti
  4616.     db     0eah
  4617. OldTimerInt dd 0         ; Self modifying code
  4618.  
  4619. @@DontChainOld:
  4620.  
  4621.     mov    al,NONSPECEOI
  4622.     out    PICCMD,al
  4623.  
  4624.  
  4625.     pop    es
  4626.     pop    ds
  4627.     popa
  4628.     sti
  4629.     iret
  4630. vsyncint endp
  4631.  
  4632.  
  4633. xinstallvsynchandler proc far VrtSkipCount:word
  4634.  
  4635.     mov     ax,[VrtSkipCount]
  4636.     or      ax,ax
  4637.     jnz     @@NonZeroCount
  4638.     mov     ax,1
  4639. @@NonZeroCount:
  4640.     mov     [VrtsToSkip],ax
  4641.     mov     [ElapsedVrts],0
  4642.     cmp     [VsyncHandlerActive],TRUE      ;Is it already active
  4643.     je      short @@Return
  4644.     call    getvsyncperiod                ;no, get the vsync period
  4645.  
  4646.     mov     [VsyncPeriod],ax
  4647.     sub     ax,INTINADVANCE               ;We need a little extra
  4648.     mov     [ClockRate],ax                  ;time
  4649.  
  4650.     mov     dx,18                           ;dx:ax=1193000
  4651.     mov     ax,13352
  4652.     idiv    [VsyncPeriod]
  4653.     mov     [TicksPerSecond],ax            ;1193/VsyncPeriod
  4654.  
  4655.     mov     word ptr [VsyncIntTicks],0
  4656.     mov     word ptr [VsyncIntTicks+2],0
  4657.     mov     MouseRefreshFlag, 0
  4658.     mov     StartAddressFlag, 0
  4659.     mov     VSyncPaletteCount, 0
  4660.  
  4661.     cli
  4662.     
  4663. IFDEF _DPMI_
  4664.     mov     ax, dpmiGetInt
  4665.     mov     bl, TIMERVECT
  4666.     int     DPMI                          ;Get Pmode vector
  4667.     mov     ax, cs                        ;vector = cx:dx
  4668.     add     ax, SelectorInc               ;Make Es = Cs so we can write to it
  4669.     mov     es, ax
  4670.     mov     word ptr es:[OldTimerInt],dx
  4671.     mov     word ptr es:[OldTimerInt+2],cx
  4672. ELSE
  4673.     mov     ax, DOSGETVECT+TIMERVECT      ;Get address of old timer int
  4674.     int     21h                           ;vector = es:bx
  4675.     mov     ax,es
  4676.     mov     word ptr cs:[OldTimerInt],bx       ;Store in OldTimerInt
  4677.     mov     word ptr cs:[OldTimerInt+2],ax
  4678. ENDIF
  4679.     mov     [VsyncHandlerActive],TRUE      ;Mark handler as active
  4680. IFDEF _DPMI_
  4681.     mov     ax, dpmiSetInt
  4682.     mov     bl, TIMERVECT
  4683.     mov     dx, offset vsyncint
  4684.     mov     cx, cs
  4685.     int     DPMI
  4686. ELSE
  4687.     mov     ax,DOSSETVECT+TIMERVECT       ;Set the new timer int
  4688.     push    ds
  4689.     mov     dx,cs
  4690.     mov     ds,dx
  4691.     mov     dx,offset vsyncint
  4692.     int     21h
  4693.     pop     ds
  4694. ENDIF
  4695.     mov     al,TIMERMODE                   ;Reprogram timer 0
  4696.     out     TIMERCONTROL,al
  4697.     mov     ax,ClockRate
  4698.     out     TIMER0,al
  4699.     mov     al,ah
  4700.     out     TIMER0,al
  4701.     sti
  4702. @@Return:
  4703.     ret
  4704.  
  4705. xinstallvsynchandler endp
  4706.  
  4707. xremovevsynchandler proc far
  4708.  
  4709.     cmp     [VsyncHandlerActive],FALSE
  4710.     je      short @@Return
  4711.     mov     dx, word ptr cs:[OldTimerInt]
  4712.     mov     cx, word ptr cs:[OldTimerInt+2]
  4713. IFDEF _DPMI_
  4714.     mov     ax, dpmiSetInt
  4715.     mov     bl, TIMERVECT
  4716.     int     DPMI
  4717. ELSE
  4718.     push    ds
  4719.     mov     ds,cx
  4720.     mov     ax,DOSSETVECT+TIMERVECT       ;Restore the old timer int
  4721.     cli
  4722.     int     21h
  4723.     pop     ds
  4724. ENDIF
  4725.     mov     al,TIMERMODE                   ;Restore timer 0
  4726.     out     TIMERCONTROL,al
  4727.     mov     al,0
  4728.     out     TIMER0,al
  4729.     out     TIMER0,al
  4730.     sti
  4731. @@Return:
  4732.     ret
  4733.  
  4734. xremovevsynchandler endp
  4735.  
  4736.  
  4737. xsetuservsynchandler proc far handlerproc:dword
  4738.     mov     ax, word ptr [handlerproc]
  4739.     mov     dx, word ptr [handlerproc+2]
  4740.     cli
  4741.     mov     word ptr [UserVsyncHandler],ax
  4742.     mov     word ptr [UserVsyncHandler+2],dx
  4743.     sti
  4744.     ret
  4745. xsetuservsynchandler endp
  4746.  
  4747.  
  4748. END
  4749.